最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

运维笔记admin8浏览0评论

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

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

我有一个异步功能,我称之为。该功能对我web3.js sendRawTransaction的AWAIT。我有一个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已经走了,之前我看到“图像发送”。但是,我得到的相反。

回答如下:

林不知道,但是你用异步/等待并在相同的操作回调。尝试重构是这样的:

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

评论列表(0)

  1. 暂无评论