0

So I've the following code which shows the working of the event loop:

console.log('scritp start')

setTimeout(() => console.log('setTimeout'), 0)

Promise.resolve().then(() => console.log('promise1')).then(() => console.log('promise 2'))

console.log('script end')

I understand how it prints 'script start' and 'script end' initially but I don't understand why Promise.resolve is executed before the setTiemout function. Aren't both of these asynchronous? As setTimeout has a time out of 0, it should execute immediately once all the synchronous events execute. Then why it executes at last?

dipansh
  • 331
  • 3
  • 15

0 Answers0