Take an example of a voting DApp. A user clicks on a vote button, then behind the scenes the transaction containing the vote gets mined on the blockchain. The DApp then wants to alert('You have voted').

Since the transaction takes some time to get mined and get some confirmations, what code should the DApp do before it does the alert? I have heard of people running loops and calling getTransactionReceipt. Is this how it should be done, or alternatives? Are there any code snippets or libraries for them?

2 Answers 2


In web3.js, .sendTransaction() has a callback.


More about the callback: https://github.com/ethereum/wiki/wiki/JavaScript-API#using-callbacks

The callback is typically called twice, once when the transaction hits the network, and again once it has been mined.

Also, using alert() is generally discouraged from javascript as it is a blocking call and goes against the asynchronous nature of javascript. (Better would be to use a Bootstrap popup for instance.)

  • alert is just for example purpose ;) Upvoting but code snippets would be more helpful for readers. Also, "callback is typically called twice"? Snippet for detecting both cases would be helpful.
    – eth
    Feb 9, 2016 at 11:09
  • I too would like to know how to call the right one at the right time.
    – max
    Apr 30, 2016 at 18:43
  • "The callback is typically called twice" is inaccurate. .sendTransaction() only calls the callback once (the callback for .new is what is called twice). ethereum.stackexchange.com/questions/7407/…
    – eth
    Jul 25, 2016 at 3:52

I have made available a complete working code example here to tell if your transaction has been mined:

How do I know when I've run out of gas

(It also accounts for if you run out of gas :-)

Note that if the node you are talking to is untrustworthy this isn't 100% reliable. How do you know it's not lying? You should really check to make sure the transaction was mined on a sample of nodes. AFAICT I have yet to see example code of this in the Ethereum world. There are such examples in bitcoin. I plan on implementing such code when I go to production (aka using real money)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.