skip to Main Content

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


  1. Are these promises executing parallelly?

    Yes. The code execution doesnt stopped on pr.
    pr and pr2 are called at almost same time

    Login or Signup to reply.
  2. "Are these promises executing parallelly?"

    Yes.

    These operations begin executing when you create them, here:

    const pr= new Promise((res,rej) =>{
        setTimeout(()=>{res("promise Execution done")},5000);
    });
    
    const pr2= new Promise((res,rej) =>{
        setTimeout(()=>{res("promise Execution done222222")},10000);
    });
    

    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:

    const val1 = await pr;
    

    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:

    const val2 = await pr2;
    

    To achieve the functionality you’re looking for, you can change pr and pr2 into functions which create and return Promises. For example:

    const pr = () => new Promise((res,rej) =>{
        setTimeout(()=>{res("promise Execution done")},5000);
    });
    
    const pr2 = () => new Promise((res,rej) =>{
        setTimeout(()=>{res("promise Execution done222222")},10000);
    });
    

    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:

    async function handelPr(){
        const val1 = await pr(); // <-- here
        console.log("Hello");
        console.log(val1);
    
        const val2 = await pr2(); // <-- and here
        console.log("Hello-->");
        console.log(val2);
    }
    handelPr()
    

    In this case the Promise from pr2 isn’t created until pr2() is invoked, which would be after the initial 5 seconds.

    Login or Signup to reply.
  3. Are these promises executing parallelly?

    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 first setTimeout() call is made before the second promise is even created. Then the second callback executes, which also executes setTimeout(). Finally, the synchronous script executes handelPr(), which in turn suspends on the first await.

    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 those setTimeout 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search