Async/await web3.js 回调中的订单问题

Async/await order issue in web3.js callback



问题是我正在一个接一个地使用 2 个异步函数。他们每个人都从智能合约中调用方法,并在 'receipt' 回调上做其他工作人员。

密码是:

await first()
await second()

let first = async function () {
   await myContract.methods.methodOne()
   .send({from: account})
   .on('receipt', async () => {
    console.log('1')
    async someAsyncFunction()
   })
}

let second = async function () {
   await myContract.methods.methodOne()
   .send({from: account})
   .on('receipt', async () => {
    console.log('2')
    console.log(variableFromContract) // undefined
   })
}

let someAsyncFunction = async function () {
   setTimeout(() => {
      variableFromContract = 10;
   }, 2000);
}

someAsyncFunction 有什么问题?
为什么在 second() 函数之前不是 运行?

提前致谢。 (我使用的是 web3.js 1.0.0-beta.37 版本)

经过多次尝试我找到了答案,不需要在 'receipt' 回调中放入异步方法,只需使用

...
.on('receipt', () => {
    console.log('block mined') 
})
.then( async () => { 
    await someAsyncFunction() // put the code here
})

这里有几个问题:

let first = async function () {
   await myContract.methods.methodOne()
   .send({from: account})
   .on('receipt', async () => {
    console.log('1')
    async someAsyncFunction() // <--- should be return await someAsyncFunction() so you have a value to pass onto second
   })
}

let second = async function () {
   await myContract.methods.methodOne()
   .send({from: account})
   .on('receipt', async () => { // <-- this is an async function but you don't await anything inside of it
    console.log('2')
    console.log(variableFromContract) // undefined
   })
}

let someAsyncFunction = async function () { // again async function but awaiting nothing to resolve.
   setTimeout(() => {
      variableFromContract = 10;
   }, 2000);
}