led them to not be macrotasks? I can’t find an explanation online
https://javascript.info/event-loop
I totally get that. I mean what’s the motivation behind adding promises to the microtask queue rather than the task queue, since the task queue seems quite suitable too
Microtasks are usually scheduled for things that should happen straight after the currently executing script, such as reacting to a batch of actions, or to make something async without taking the penalty of a whole new task. The microtask queue is processed after callbacks as long as no other JavaScript is mid-execution, and at the end of each task. Any additional microtasks queued during microtasks are added to the end of the queue and also processed. Once a promise settles, or if it has already settled, it queues a microtask for its reactionary callbacks. This ensures promise callbacks are async even if the promise has already settled. So calling .then(yey, nay) against a settled promise immediately queues a microtask. This is why promise1 and promise2 are logged after script end, as the currently running script must finish before microtasks are handled. promise1 and promise2 are logged before setTimeout, as microtasks always happen before the next task.
I guess to make us use promises even when we are not performing any asynchronous task and want it to execute at the immediate future
Обсуждают сегодня