I've seen similar questions here with explanations, but can't quite make this work.

Using ethers.js, I have a function that makes a transaction on the smart contract. I am listening to an event below to extract a parameter, but then outside of the Promise it is undefined:

let orderHash;
myContract.on("MakeOrder", (order, hash) => {
  orderHash = hash;

console.log(orderHash) // logs undefined

How to properly extract the value?

1 Answer 1


Ended up with this working code:

const makeOrderEvent = new Promise((resolve, reject) => {
  myContract.on("MakeOrder", (order, hash, event) => {

      order: order,
      hash: hash,

  setTimeout(() => {
    reject(new Error("timeout"));
  }, 60000);

const event = await makeOrderEvent;

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.