I'm confused which task is taken first by event loop.
The reason is like below.
Stackoverflow answer from this
After this macrotask has finished, all available microtasks will be processed
Similar example from this blog article
setTimeout(() => console.log('Macro task'), 0);
Promise.resolve().then(() => console.log('Micro task'));
So, it is very confusing for me. What I understood till now is this.
setTimeout() is inserted into Macro Task Queue.Promise is inserted into Micro Task Queue.This process is my understanding and am I right?
Micro-task queue is checked right after the script finishes, I think. It is before the checking of macro-task queue.
See here for detailed information.
Because the script itself is treated as a macrotask so that at the end the enqueued microtasks are executed.
So the promise is a microtask, setTimeout callback is a macrotask, but the script is a macrotask as well.
As a result, script -> promise -> setTimeout
More details here: https://medium.com/javascript-in-plain-english/javascript-event-loop-y-promises-951ba6845899
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