Changes to the Timers and Microtasks in Node v11.0.0 ( and above)
Tags: node, javascript
Published: 2019-12-06 21:51:17

This article is a short follow up to my original article series about Node.js Event Loop. In the original article series, I discussed in detail about Timers, setImmediate, process.nextTick, Promises and many more.

However, since Node.js v11.0.0, there are some significant changes to the behavior of setTimeout, setImmediate, process.nextTick and Promises. In this article, I’m going to discuss these new changes along with some comparisons of the functionality between Node < v11.0.0 and Node ≥ v11.0.0. If you miss any of my previous articles about Node.js Event loop, I recommend you read them from the following links and return here to see the new changes introduced in Node v11.0.0.

- [Event Loop and the Big Picture](https://blog.insiderattack.net/event-loop-and-the-big-picture-nodejs-event-loop-part-1-1cb67a182810)
- [Timers, Immediates and Next Ticks](https://dev.to/deepal/timers-immediates-and-process-nexttick-nodejs-event-loop-part-2-3ejj-temp-slug-6807715)
- [Promises, Next-Ticks, and Immediates](https://blog.insiderattack.net/promises-next-ticks-and-immediates-nodejs-event-loop-part-3-9226cbe7a6aa)
- [Handling I/O](https://dev.to/deepal/handling-io-nodejs-event-loop-part-4-4f9b-temp-slug-3210704)
- [Event Loop Best Practices](https://dev.to/deepal/event-loop-best-practices-nodejs-event-loop-part-5-1mbj-temp-slug-1328352)
- **New changes to timers and microtasks in Node v11 (This article)**

#### Rationale

If you run the following piece of code in the browser and node separately, you’ll get contradicting results.

{% gist https://gist.github.com/deepal/60d50579c71484e2708e847513b0822b %}

In the browser, you will get:

```
timeout1
timeout2
promise resolve
timeout3
timeout4
```

However, in Node versions below 11.0.0, you’ll get the following output:

```
timeout1
timeout2
timeout3
timeout4
promise resolve
```

In Node JS implementation, process.nextTick callbacks and microtasks (e.g, promise callbacks) were executed between each phase of the event loop when the C++/JavaScript boundary is crossed. Therefore, all timer callbacks are executed in the timers phase of the event loop before the Promise callback is executed which resulted in the above output.

However, this contradicting output between the browser and Node has been [under discussion](https://github.com/nodejs/node/issues/22257) for a while and [a feature (or a fix) has been landed in Node.js v11.0.0](https://github.com/nodejs/node/pull/22842) to follow the browser behavior. With this feature, Node.js v11.0.0 or above will output the following which matches the browser’s output:

```
timeout1
timeout2
promise resolve
timeout3
timeout4
```

See the following comparison between Node v10.15.1 and Node v11.10.0:

![](https://cdn-images-1.medium.com/max/1024/1*Nv_NkCqBiksw_dsplsidVg.png)<figcaption>Different outputs in Node v10 and v11</figcaption>

This change not only affects setTimeout, but also affectssetImmediate. Let’s try to run the following code in Node v10 and Node v11 and see how the output is different.

{% gist https://gist.github.com/deepal/ef810497b68231690555b6f5bdd2940b %}

Node v10 and Node v11 clearly give two different outputs as follows:

![](https://cdn-images-1.medium.com/max/1024/1*iHq0yePR1XEY2o5tjX4EGw.png)

This behavior is quite the same if you replace `Promise.resolve().then` with `process.nextTick` because microtasks are run after the process.nextTick callbacks are run. Let’s try to run the following snippet:

{% gist https://gist.github.com/deepal/4a2afba0f1bf651befacfcfa6a86fc95 %}

The output of Node v10 and Node v11 for the above script is as follows:

![](https://cdn-images-1.medium.com/max/1024/1*EZQI-PmMq70FKClrwh5Xsg.png)

#### What happens here?

With the new changes in Node v11, nextTick callbacks and microtasks will run between each individual setTimeout and setImmediate callbacks, even if the timers queue or the immediates queue is not empty. In terms of setTimeout and Promise callbacks, the new changes in Node v11 match the browser behavior which improves the reusability of browser JavaScript in Node.js. However, this significant change could potentially break existing Node.js applications which explicitly rely on the old behavior. Therefore, if you are upgrading to Node v11 or above (preferably the next LTS v12), you might need to consider this as important.

**References:**

- MacroTask and MicroTask execution order [https://github.com/nodejs/node/issues/22257](https://github.com/nodejs/node/issues/22257)
- PR: timers: run nextTicks after each immediate and timer [https://github.com/nodejs/node/pull/22842](https://github.com/nodejs/node/pull/22842)

* * *