I have two questions from the following example:
Why is does x log before y? Why is x a Promise?
I'm expecting bar
to wait for foo
to resolve with the value 'Hello'
before it logs and then returns it.
let foo = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello');
}, 2000);
})
}
let bar = async () => {
let y = await foo();
console.log(y);
return y;
}
let x = bar();
console.log(x);
What I'm expecting to see is
'Hello' // console.log(y)
'Hello' // console.log(x)
What I'm getting is
Promise {<pending>} // console.log(x)
'Hello' // console.log(y)
Shouldn't bar
wait for foo
to resolve before returning y
?
Any help would be much appreciated!
Async functions The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically. So, async ensures that the function returns a promise, and wraps non-promises in it.
Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. Note: Even though the return value of an async function behaves as if it's wrapped in a Promise.resolve , they are not equivalent.
Wait for function to finish using async/await keywords The await keyword allows you to wait until the Promise object is resolved or rejected: await first(); second(); However, the await keyword must be used inside an async function .
Here is the documentation about async function
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
And Promise
object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
It is stated that an async function
returns a Promise
object which is in a pending
state until it is resolved.
Now let's look at your code:
You assign to x
the return value of bar()
, which is a Promise
, since there is no await
keyword to stop the execution (And you can not add one, because you are outside of an async function
) this promise is logged as pending because the async function bar()
has not yet returned.
Meanwhile, in the async function bar()
you assign to y
the return value of the async function foo()
which is also a Promise
, but this time you make it await for the result. After 2 seconds of waiting time, the promise is fulfiled and y
is logged with the parameter of the resolve()
method, which is Hello
.
Thus the behaviour you witness is the expected one.
You need to await
the function:
let example = async () => {
let foo = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello');
}, 2000);
})
}
let bar = async () => {
let y = await foo();
console.log(y);
return y;
}
let x = await bar();
console.log(x);
}
example();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With