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
    .on('confirmation', (confirmationNumber, receipt) => {
      console.log('=> confirmation: ' + confirmationNumber);
    .on('transactionHash', hash => {
      console.log('=> hash');
      //console.log("HASH: ", hash);
      transHash = hash;
    .on('receipt', receipt => {
      console.log('=> reciept');
    .on('error', console.error);

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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