使用async / await promises NodeJS获取事务哈希
我想通过运行此代码来获取事务哈希:
const transactionId = await web3.eth.sendSignedTransaction('0x' + serializedTransaction.toString('hex') ).on('receipt', function(receipt) {
return receipt.transactionHash;
});
// Now it is known the transaction ID, so let's build the public Etherscan url where the transaction details can be viewed.
const url = `/${transactionId}`
console.log(url)
代码适用于事务的关注,我可以在Etherscan上看到它们。问题是关于Promises的JavaScript我们。
我在这种情况下控制台注销:
/[object Object]
我尝试了不同的方法来获取事务哈希但没有成功。你能帮助我吗?它也可能是更好地理解Promise如何运作的合适机会。
回答如下:你正在将promises与事件emmiters结合起来(这是可能的),但我建议在事件基于emmiter的方法之后尝试第一个基于promise的。之后你可以试验混合物。 :)
- 承诺:
async function fetch(){ const transactionID = await web3.eth.sendSignedTransaction('0x'+serializedTransaction.toString('hex'));return transactionID; }let a = fetch() a.then(response=>console.log('transactionHash => ' + response) .catch(error => console.log('error with sending transaction => ' + error);
- 基于事件发射器: qazxsw poi