Search code examples
node.jsnpmargon2-ffi

Node.js argon2 Package Will Not Return String


I am writing a web app that requires password hashing. I am using the argon2 package from npm to achieve this.

Below is a function that I have written that is ment to return a string such as $argon2i$v=19$m=4096,t=3,p=1$QC3esXU28vknfnOGCjIIaA$f/2PjTMgmqP1nJhK9xT0bThniCEk28vX2eY6NdqrLP8 but insted the function returns Promise { <pending> } when the value is console.log(ed).

The code is:

async function hashPassword(password) {
    try {
        const hash = await argon2.hash(password);
        return hash;
    } catch {
        console.log('Error');
    }
}
const hashedPassword = hashPassword('password');
console.log(hashedPassword);

So, the output from the console.log() is Promise { <pending> }

Can someone please assist me in solving this problem?

Thank you very much.


Solution

  • You need await when call hashPassword():

    async function hashPassword(password) {
        try {
            const hash = await argon2.hash(password);
            return hash;
        } catch {
            console.log('Error');
        }
    }
    const hashedPassword = await hashPassword('password');
    console.log(hashedPassword);