How to wait for a transaction to complete using Ethers JS

In this tutorial, we are going to learn how to wait for a transaction to complete (either it fails or succeeds) using Ethers JS and JavaScript.

In this tutorial, we are going to learn how to wait for a transaction to complete (either it fails or succeeds) using Ethers JS and JavaScript.

To wait for a transaction to complete, we can use the waitForTransaction function or the wait method of a transaction object.

Using waitForTransaction

Here is an example with waitForTransaction:

const ethers = require("ethers")

const provider = new ethers.providers.Web3Provider(YOUR_PROVIDER_HERE)

const transactionHash = "0x331cd2ded0f91e6391786fc6aacd78a355e07bf5bcd099012e4016a48b09c5f7"

provider.waitForTransaction(transactionHash)
.then((receipt) => {
    console.log(receipt)
})
.catch((error) => {
    console.log(error)
})

That function takes in parameters:

  • the hash of the transaction to wait for
  • the number of confirmations to wait for (defaults to 1)

It returns a promise and that promise resolves when the transaction is completed successfully or rejects if the transaction fails.

If the transaction is successful, it resolves and returns the transaction receipt, otherwise it rejects and gives you an error.

To learn more about the transaction receipt object, check out the documentation here: https://docs.ethers.io/v5/api/providers/types/#providers-TransactionReceipt

Or check out our tutorial on how to get the state of a transaction, at the bottom I explain in details what the transaction receipt object looks like:

How to get the state of a transaction using Ethers JS and JavaScript
In this tutorial, we are going to learn how to get the state of a transaction using Ethers JS and JavaScript.

Using the wait method

When you call getTransaction or sendTransaction, you get back a TransactionResponse object that has a wait method.

Calling that function allows you to wait for the transaction to be completed on the blockchain and receive a transaction receipt when it's done.

Here is an example of how to use it:

const ethers = require("ethers")

const provider = new ethers.providers.Web3Provider(YOUR_PROVIDER_HERE)

const transactionHash = "0x331cd2ded0f91e6391786fc6aacd78a355e07bf5bcd099012e4016a48b09c5f7"

getTransaction(transactionHash)
.then((transaction) => {
    provider.wait()
    .then((receipt) => {
        console.log(receipt)
    })
    .catch((error) => {
        console.log(error)
    })
})

The wait function takes in parameter the number of confirmations on the transaction that you want to wait for, the default value is 1.

The wait function returns a Promise and when the transaction is completed successfully, the promise is fulfilled and returns a transaction receipt. If the transaction failed, it returns an error (that contains the transaction receipt as well, but the status property indicates that the transaction failed).

Again, to learn more about the receipt object, check out the documentation here: https://docs.ethers.io/v5/api/providers/types/#providers-TransactionReceipt

Or check out this tutorial, at the very bottom:

How to get the state of a transaction using Ethers JS and JavaScript
In this tutorial, we are going to learn how to get the state of a transaction using Ethers JS and JavaScript.

You can also read the tutorial above to learn more about the getTransaction function.

For more information about the wait function, check out the documentation here and scroll down a little bit to see the wait function.

The error object is explained there and it's super useful to handle errors properly.

And that's it 🎉

Thank you for reading this article