1

(1) this is apparently code that hashes a password. however what does it exactly do syntactically? why does it use async and await this way? and => does not seem to define a function.

const hashPassword = async password => await bcrypt.hash(password, 10)

(2) how do you return a value in an async function? i have an async function returning an array however when called it is received as a Promise object..

async function agentname() { 
  // ...
  return an_array;
}

let data = agentname();
console.log("in callback. data is " + data);

Output:

in callback. data is [object Promise]

Is the problem in how i define the function/return value or how the function is called?

hyperbolicme
  • 29
  • 1
  • 8
  • "*and => does not seem to define a function*" why do you say that? `async password => await bcrypt.hash(password, 10)` is an async arrow function that takes a parameter `password` and returns `bcrypt.hash(password, 10)` after awaiting the result. Presumably, `bcrypt.hash` is also async. – VLAZ Nov 05 '19 at 11:02
  • `i have an async function returning an array however when called it is received as a Promise object` that's what async functions always do - they are supposed to be asynchronous, so the result will come *at a later time*. They always return a Promise. You either `await` it or use the promise API and use `.then` on it. – VLAZ Nov 05 '19 at 11:03
  • i see. i expected parenthesis to be around the parameters in this form of definition.. ok so if this is a function then the placement of await makes sense. (i am fairly new to javascript. ) – hyperbolicme Nov 05 '19 at 11:13
  • In arrow functions, if you have a single parameter, you can omit the brackets around it: `(arg) => arg + 1` is the same as `arg => arg + 1`. If you have zero or more than one parameters, then you cannot omit them: ` => 1` is invalid by itself, you need `() => 1` also (arg1, arg2) => arg1 + arg2` cannot be written as `arg1, arg2 => arg1 + arg2` (technically, its valid syntax but has different semantics) – VLAZ Nov 05 '19 at 11:17
  • using `await` would mean preceding the call with `await` and preceding the defining function with `async`? that seemed to work. thanks! – hyperbolicme Nov 05 '19 at 11:23

1 Answers1

0

this is apparently code that hashes a password. however what does it exactly do syntactically? why does it use async and await this way? and => does not seem to define a function.

It does define a function and assigns hashPassword to the function. It's the same thing as this:

const hashPassword = async function( password ) {
    return await bcrypt.hash( password, 10 );
};

...or this (just to be clear):

const hashPassword = async function( password ) {
    const hashedPassword = await bcrypt.hash( password, 10 );
    return hashedPassword;
};

and also largely (for the most part) the same thing as doing this:

async function hashPassword( password ) {
    const hashedPassword = await bcrypt.hash( password, 10 );
    return hashedPassword;
}

...which is the same thing as this (ignoring error-handling with rej):

function hashPassword( password ) {
    return new Promise( ( res, rej ) => bcrypt.hash( password, 10 ).then( h => res( h ) )
}

how do you return a value in an async function? i have an async function returning an array however when called it is received as a Promise object..

When a function is annotated with async it wraps the return value in a Promise<T>. Because of how types change behind-the-scenes when using async functions in JavaScript I strongly recommend using TypeScript instead of JavaScript to avoid errors like these. Note that you can use await with both Promise and non-Promise functions, at least.

In JavaScript, these two functions are equivalent:

async function foo() {
    const v = await anotherAsyncFunction();
    return v;
}

function foo() {
    return new Promise( function(resolve,reject) {
        anotherAsyncFunction().then( v => resolve( v ) );
    } );
}

When a function returns a Promise<T> can you can either await it to get T or you use a then function.

See here: How to return values from async functions using async-await from function?

Dai
  • 141,631
  • 28
  • 261
  • 374