0

Below is my code snippet. I wish to wait for the transaction hash value so that I can return my Javascript function with the hash value.

But I get the error "SyntaxError: await is only valid in async functions and async generators" in Firefox console. I'm using web3js 1.0 at https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.34/dist/web3.min.js'

web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
  const tran = await web3.eth
    .sendSignedTransaction(signed.rawTransaction)
    .on('confirmation', (confirmationNumber, receipt) => {
      console.log('=> confirmation: ' + confirmationNumber);
    })
    .on('transactionHash', hash => {
      console.log('=> hash');
      console.log(hash);
      //console.log("HASH: ", hash);
      transHash = hash;
    })
    .on('receipt', receipt => {
      console.log('=> reciept');
      console.log(receipt);
    })
    .on('error', console.error);
});

closed as off-topic by Ismael, alberto, shane, Majd TL, Niklas Feurstein Oct 17 at 12:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – Ismael, alberto, shane, Majd TL, Niklas Feurstein
If this question can be reworded to fit the rules in the help center, please edit the question.

  • you need to wrap that in a promise – Majd TL Oct 16 at 15:01
1

Change this:

signed => {

To this:

function(signed) {

And then to this:

async function(signed) {

The middle phase is just an intermediate step in order clarify the syntax for you.

0

You could also do this:

async (signed) => {

if you want to use shorthand notation.

Not the answer you're looking for? Browse other questions tagged or ask your own question.