I am new to web3 developing in java script. I am a little confused when to use a call back function, and when to use a promise. For instance this code to get transaction hash can be written in may ways:

web3.eth.sendSignedTransaction(transaction, (err, hash) => {console.log(hash)})

can also we written as:

web3.eth.sendSignedTransaction(transaction).on('receipt' , console.log)

I want to know the logic behind every way, and what is the difference between using a promise, and a call-back function. There is also the .then() scheme, and the on scheme. I am lost in the details, and can't know the difference between them, and how they work. The only thing I can see is that they can be used interchangeably.


1 Answer 1


The callback function is executed when the Node sends the transaction hash back to your process.

This typically happens almost immediately, and as such, it does not indicate that the transaction has been mined; you still need to take the transaction hash and use it in order to obtain the receipt; furthermore, you still need to take that receipt and wait for several confirmations of your transaction (where 12 confirmations are considered as a proof of your transaction permanently added to the blockchain).

The on function allows you to wait for the transaction hash or for the receipt or for each confirmation.

So it generally provides a wider range of options; see this coding example (it is given for function sendTransaction, but I believe that it works the same for function sendSignedTransaction).

Here is a better explanation that I've come up with:

Function sendSignedTransaction returns a Promise object.

This object has an on function which can handle either one of the following events:

  • 'transactionHash'
  • 'receipt'
  • 'confirmation'
  • 'error'

If you pass a callback to function sendSignedTransaction, then before this function returns the Promise object, it handles the 'transactionHash' and 'error' events for you.

So in essence, this:

web3.eth.sendSignedTransaction(transaction, (err, hash) => {console.log(hash)})

Is equivalent to this:

    .on('transactionHash', (hash) => {console.log(hash)})
    .on('error', (err) => {})

Note that this "chaining" of on function-calls is made possible by the fact that the on function returns the Promise object after handling the specified event.

  • Hello. Thanks for your reply. How about then() ? It seems to do the same, too.
    – Crypto
    Commented Nov 26, 2019 at 16:01
  • @Crypto: This is a general Javascript question rather than a specific Ethereum (web3) question. I've never used then, and I hardly ever use on (I just go for the clean async/await scheme). So I recommend that you post it on Stack Overflow instead. Commented Nov 26, 2019 at 16:04
  • @Crypto: The part about sendSignedTransaction is nevertheless a good question for this website, because the official documentation is indeed rather incomplete, and doesn't seem to cover all of these different usage options. So I'm not really sure why people here have voted to close it. I believe that the first one may have missed this fact, and the others just follow like a herd without even taking the time to think it through. Commented Nov 26, 2019 at 16:08
  • I know right, maybe because of the question title, it seems to be very general.
    – Crypto
    Commented Nov 26, 2019 at 16:19

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.