I am working with the promises and async function. The thing I got to know that inside the async function if there is a await out JS engine will suspend the ongoing call stack’s function calls and wait for the awaited promise to resolve.
The Question is, let me put the code first:-
const pr = new Promise((res, rej) => {
setTimeout(() => {
res("promise Execution done")
}, 5000);
});
const pr2 = new Promise((res, rej) => {
setTimeout(() => {
res("promise Execution done222222")
}, 10000);
});
async function handelPr() {
const val1 = await pr;
console.log("Hello");
console.log(val1);
const val2 = await pr2;
console.log("Hello-->");
console.log(val2);
}
handelPr()
Here what i can see on the console that after 5sec pr resolved and returned the value but after another 5secs pr2 returned the value but i thing that after pr returned the value, pr2 should take 10sec not 5sec which is the current scene. What i am assuming the whole process should take 15sec not 10sec. Are these promises executing parallelly? Or i am missing something from the event loop?
Another thing if I change the timings of pr to 10sec and pr2 to 5sec then also it is taking 5sec.
Please let me understand, Thanks in advance!!..
3
Answers
Yes. The code execution doesnt stopped on pr.
pr and pr2 are called at almost same time
Yes.
These operations begin executing when you create them, here:
So those timeouts are created immediately (and close enough to simultaneously that any difference doesn’t matter). One will execute 5 seconds from right now, the other will execute 10 seconds from right now.
So when you await the first one:
That will wait until that 5 seconds has elapsed. But that 5 seconds will have elapsed for both Promises. So there’s only 5 seconds remaining on the second one:
To achieve the functionality you’re looking for, you can change
pr
andpr2
into functions which create and return Promises. For example:These lines of code don’t create (and subsequently invoke) the Promises immediately, not until these functions are actually called. When you want to await them, call the functions and await the results:
In this case the Promise from
pr2
isn’t created untilpr2()
is invoked, which would be after the initial 5 seconds.Promises don’t execute. They are objects, not functions.
The callback that you passed to
new Promise
executes immediately (synchronously), so in your case that firstsetTimeout()
call is made before the second promise is even created. Then the second callback executes, which also executessetTimeout()
. Finally, the synchronous script executeshandelPr()
, which in turn suspends on the firstawait
.But at that time both timers are already scheduled (this is dependent on non-JavaScript code). As both
setTimeout
calls were made at almost the same time, their timeouts do not add up: the first will expire after about 5 seconds from when thosesetTimeout
calls were made, and the other about 10 seconds from that same point in time.In your script there is no concurrent execution of JavaScript code. It is just that the two timers started their "count down" at about the same time.