0

I'm implementing argon2 in my application for password.

try {
  if (await argon2.verify("<big long hash>", "password")) {
    // password match
  } else {
    // password did not match
  }
} catch (err) {
  // internal failure
}

But getting an error SyntaxError: await is only valid in async function. How can i use async funtion?

Victory
  • 1,184
  • 2
  • 11
  • 30
  • You can only await async function within an async function. e.g `async function verifyPassword(password){ if(await argon2.verify("", "password")){/*...*/} }` – Ricky Mo May 31 '19 at 08:26

1 Answers1

3

You have to declare an async function with async keyword:

const myAsyncFunc = async (hashKey, password) => { //<-- declare as an async function
  try {
    if (await argon2.verify(hashKey, password)) {
      // password match
    } else {
      // password did not match
    }
  } catch (err) {
    // internal failure
  }
}

myAsyncFunc("<big long hash>", "password");
  • Updated answer to make it more verbose
iKoala
  • 870
  • 4
  • 11
  • @iKola, Its not working for me :( , Can I make a helper function(async) using above code? – Victory May 31 '19 at 09:44
  • @VijayKumar yes, you can create a helper function to do that. You just have to wrap your `await` code inside a function which is declared as `async` function. You cannot run `await` function call without wrapping it into an async function. – iKoala May 31 '19 at 10:41
  • @VijayKumar I updated the answer for a bit to make it more clear how it works. – iKoala May 31 '19 at 10:43