Consider the below code,
// Function which returns a promise and resolves in 2 seconds
const promiseFunction = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
};
// Asynchronous function 1
const asyncFunction1 = async() => {
console.log(await promiseFunction());
console.log(await promiseFunction());
console.log(await promiseFunction());
}
// Asynchronous function 2
const asyncFunction2 = async() => {
let a = promiseFunction();
let b = promiseFunction();
let c = promiseFunction();
console.log(await a);
console.log(await b);
console.log(await c);
}
I am trying to understand the behaviour between asyncFunction1 and asyncFunction2 execution, the asyncFunction1 takes 2 seconds for each await (total 6) when not assigned to a variable, but asyncFunction2 resolves all the 3 promises in total 2 seconds when assigned to a variable, what happens here when it is assigned to a variable? (and we still use await with the variable).
The await keyword is used to get a value from a function where you would normally use . then() . Instead of calling . then() after the asynchronous function, you would simply assign a variable to the result using await .
Awaiting a promise to be fulfilled If a Promise is passed to an await expression, it waits for the Promise to be fulfilled and returns the fulfilled value.
Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.
If you use the async keyword before a function definition, you can then use await within the function. When you await a promise, the function is paused in a non-blocking way until the promise settles. If the promise fulfills, you get the value back. If the promise rejects, the rejected value is thrown.
The timers start when you call promiseFunction()
, thus when you await a
, all three timers are already running. Then when a
finishes after 2 seconds, the other ones are already done too.
The callback function passed in the Promise constructor is started to execute when the promise is created(This is the main difference between Observable and Promise). Please take a look at the first code.
// Asynchronous function 1
const asyncFunction1 = async() => {
console.log(await promiseFunction());
console.log(await promiseFunction());
console.log(await promiseFunction());
}
This function waits for 2 minutes for each function so it would take 6 seconds. While the second function doesn't work the same way.
// Asynchronous function 2
const asyncFunction2 = async() => {
let a = promiseFunction();
let b = promiseFunction();
let c = promiseFunction();
console.log(await a);
console.log(await b);
console.log(await c);
}
It executes the 3 callback functions simultaneously. It works exactly like await Promise.all(). Therefore asyncFunction2 would be resolved in about 2 seconds.
For
asyncFunction1
asyncFunction2
Check my below console .you can see the different
const promiseFunction = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
};
// Asynchronous function 1
const asyncFunction1 = async() => {
console.log(await promiseFunction(),1);
console.log(await promiseFunction(),1);
console.log(await promiseFunction(),1);
}
// Asynchronous function 2
const asyncFunction2 = async() => {
let a = promiseFunction();
let b = promiseFunction();
let c = promiseFunction();
console.log(await a,2);
console.log(await b,2);
console.log(await c,2);
}
asyncFunction1();
asyncFunction2();
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