0
const promiseOne = new Promise(resolve=>{
    setTimeout(()=>resolve(1), 3000)
})
const promiseTwo = new Promise(resolve=>{
    setTimeout(()=>resolve(2), 2000)
})
const promiseThree = new Promise(resolve=>{
    setTimeout(()=>resolve(3), 1000)
})

const callAllPromiseInAsync = async () => {    
     const start = Date.now()
     const p1 = await promiseOne
     const p2 = await promiseTwo
     const p3 = await promiseThree
        
     console.log([p1,p2,p3])
     console.log( Date.now() - start)
}

When I called this function, it took almost 3 seconds to run instead of 6. Shouldn't it wait 3 seconds for p1, 2 seconds for p2, and 1 second for p3, resulting in a total of 6 seconds?

Chinmoy kr
  • 69
  • 5
  • 2
    All the timers run concurrently. – Pointy May 05 '23 at 20:02
  • 2
    No. It doesn't wait 3s after you call the function. It only waits for the promises, and those fulfill 3s after you created the promise. – Bergi May 05 '23 at 20:02
  • 1
    The timers start when you create the promises, and they're all created at the same time. – tadman May 05 '23 at 20:05
  • 1
    Question closed before I could answer so here is the answer https://chat.stackoverflow.com/rooms/253502/room-for-adsy-and-chinmoy-kr – adsy May 05 '23 at 20:09
  • @adsy You can post the answer to the duplicate target. It fits there as well – Bergi May 06 '23 at 03:17

0 Answers0