I have an async function which I call. The function has an await for my web3.js sendRawTransaction
. I have a console.log
after it to test if it indeed waits. But it immediately prints before web3 is done sending the transaction. I'm suspecting that sendRawTransaction
isn't a promise, and therefore can't be called via await.
Here is the function
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..");
}
I want to see "after async await.." printed after the web3 has gone, and before I see "image is sent". But, I get the reverse.
Im not sure, but you use async/await and callback in same operation. Try to refactor like this:
const hash = await web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'))
console.log(hash)
conosle.log("after aync await..")