Search code examples
javascriptevent-loop

Micro and macro tasks in JavaScript runtime


There are micro- and macro- task queues in the JavaScript runtime.

setTimeout uses the macrotask queue.

setImmediate in IE11 presumably uses the microtask queue?

process.nextTick uses the microtask queue.

But setImmediate is not implemented in any other browser (and won't be AFAIK).

And process.nextTick is Node only.

Promises use the micro task queue. Could Promise therefore be leveraged to provide setImmediate like functionality in non IE browsers?


Solution

  • Three years later, of course, but we do have an answer for this now: the window.queueMicrotask() method has been added to allow adding a callback to the JavaScript runtime's microtask queue. I'm currently working on fleshing out a microtask guide on MDN Web Docs.