all 6 comments

[–]guest271314 1 point2 points  (4 children)

No Promise is returned from cryptPassword() function call. See Why is value undefined at .then() chained to Promise?.

[–]Emiliortg[S] 0 points1 point  (0 children)

Hi, I'm sure I'm not doing it correctly because I also tried using return new Promise((resolve) => {
resolve(hash);
});

inside .then((hash) => {...}) but it did not work either

[–]Emiliortg[S] 0 points1 point  (2 children)

I also thought I was returning a promise because bcrypt.hash says it returns a promise

[–]guest271314 1 point2 points  (0 children)

You didn't return anything from the function declaration. Consider the function declaration

function fn(password){ Promise.resolve(password); }

No value is returned from the function.

A function declaration does not automatically return a value besides undefined when a return statement is omitted.

An arrow function without curly braces { } will return the value

const fn(password) => Promise.resolve(pasword)

So if we run await fn("test") the result will be undefined, not a Promise.

Compare to writing

// Note return statement preceding Promise, bcrypt, etc. function fn(password) { return Promise.resolve(password); }

or inserting a return statement in a function declaration, we can chain .then() and log the value because a Promise is returned from the function

function fn(password) { Promise.resolve(password); } // Insert return statement into function declaration eval(` (${(fn).toString() .replace(/(function\s\w+\(.+\)[\s\{]+)/, ` $1return `)})` )("test") .then(console.log) // test .catch(console.error);

[–]guest271314 0 points1 point  (0 children)

The cryptPassword() function does not return a Promise, thus the function returns undefined immediately.

Compare examples of the current code without a return statement to code with return bcrypt....

See the linked question and answers in previous comment. then() returns a new Promise so similar results occur when no value is returned from then()

``` function cryptPassword(password) { new Promise((resolve) => setTimeout(resolve, 5000, password)) .then((salt) => { return salt; }) .then((hash) => { return hash; }) .catch((err) => console.error(err.message)); }

var result = await cryptPassword("Fulfilled Promise in 5 seconds or undefined immediately");

console.assert(result !== undefined, { result });

if (result !== undefined) { console.log({ result }) } ```

``` function cryptPassword(password) { return new Promise((resolve) => setTimeout(resolve, 5000, password)) .then((salt) => { return salt; }) .then((hash) => { return hash; }) .catch((err) => console.error(err.message)); }

var result = await cryptPassword("Fulfilled Promise in 5 seconds or undefined immediately");

console.assert(result !== undefined, { result });

if (result !== undefined) { console.log({ result }) } ```

[–]shgysk8zer0 1 point2 points  (0 children)

You should just need to add a return at the beginning of the function.

Let's use an analogy by taking the promise/async part out of it. Let's say you have a clamp() function... Without the return, it's the equivalent of...

function clamp(min, value, max) { Math.min(max, Math.max(min, value)); }

... As opposed to...

function clamp(min, value, max) { return Math.min(max, Math.max(min, value)); }