question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `prefer-queue-microtask`

See original GitHub issue

queueMicrotask() serves a similar purpose to process.nextTick() with the additional advantage that queueMicrotask() is specified in the WHATWG HTML Living Standard and supported on both modern browsers and Node.js (v11.0.0 and later).

When to use queueMicrotask() vs. process.nextTick() in the Node.js API documentation states:

For most userland use cases, the queueMicrotask() API provides a portable and reliable mechanism for deferring execution that works across multiple JavaScript platform environments and should be favored over process.nextTick(). In simple scenarios, queueMicrotask() can be a drop-in replacement for process.nextTick().

I propose adding a rule to warn when process.nextTick is used, and suggest queueMicrotask() as an alternative.

Fail

process.nextTick(() => process.exit(0));
process.nextTick(process.exit);
process.nextTick(process.exit, 1);
const fun = process.nextTick;
fun(process.nextTick);

Pass

queueMicrotask(() => process.exit(0));
queueMicrotask(process.exit);
// Note: queueMicrotask takes exactly 1 arg.
// Fail example fixed using .bind().
queueMicrotask(process.exit.bind(undefined, 1));
// Not sure if this should be auto-fixed.
// fun may be called with multiple args.
const fun = queueMicrotask;
// Not sure if this should be auto-fixed.
// fun may call its argument with multiple args.
fun(process.nextTick);

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sindresorhuscommented, Nov 25, 2021

Seems like we’ll eventually get another alternative, so we can put this rule on hold until it’s a reality:

0reactions
sindresorhuscommented, Jul 1, 2021

Ideally, we would have a rule prefer-set-immediate which would prefer it over setTimeout(..., 0), but setImmediate doesn’t work in the browser, so it’s not a good API to use.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What was the motivation for introducing a separate microtask ...
The submission was ultimately rejected as an implementation issue not belonging to the language standard. Arguments supporting the submission ...
Read more >
decorator for post-construction (after subclass constructors)
The only way we can currently run logic within a base class after construction (after the entire constructor call stack of a class ......
Read more >
Using microtasks in JavaScript with queueMicrotask()
A microtask is a short function which is executed after the function or program which created it exits and only if the JavaScript...
Read more >
The Difference Between Node.js 10 LTS and Node.js 12 LTS
Implemented experimental "pkg-exports" proposal. A new "exports" field can be added to a module's package.json file to provide custom ...
Read more >
The Shareholder Proposal Rule: A Cornerstone of Corporate ...
The Shareholder Proposal Rule. First, I would like to provide a little background on Rule 14a-8, which I am sure is familiar to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found