为什么我的异步等待不等待我的 web3.js sendRawTransaction?

How come my async await doesn't wait my web3.js sendRawTransaction?

我调用了一个异步函数。该函数等待我的 web3.js sendRawTransaction。我在它后面有一个 console.log 来测试它是否确实在等待。但它会在 web3 完成发送交易之前立即打印。我怀疑 sendRawTransaction 不是一个承诺,因此不能通过 await 调用。

这是函数

async function sendImage() {
var count = web3.eth.getTransactionCount(Wallet1);

var rawTransaction = {
    "from": Wallet1,
    "nonce": web3.toHex(count),
    "gasPrice": "0x2540BE400",
    "gasLimit": "0x3A980",
    "to": imageContract,
    "value": "0x0",      
    "data": imageContractABI.startGame.getData(2, {from: Wallet1}),
    "chainId": 0x04
};


var privKey = new Buffer (PrivKey1, 'hex');
var tx = new Tx(rawTransaction);


tx.sign(privKey);
var serializedTx = tx.serialize();


await web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
    if (!err){
      console.log("Image is sent " + hash);
    }
    else
        console.log(err);

}); 

console.log("after aync await..");

}

我想在 web3 消失后看到 "image is sent" 之前打印 "after async await.."。但是,我得到了相反的结果。

我不确定,但是您在同一操作中使用了 async/await 和回调。尝试这样重构:

const hash = await web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'))
console.log(hash)
conosle.log("after aync await..")