Rule proposal: `prefer-queue-microtask`
See original GitHub issuequeueMicrotask()
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 overprocess.nextTick()
. In simple scenarios,queueMicrotask()
can be a drop-in replacement forprocess.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:
- Created 2 years ago
- Reactions:2
- Comments:9 (6 by maintainers)
Top GitHub Comments
Seems like we’ll eventually get another alternative, so we can put this rule on hold until it’s a reality:
scheduler.yield()
: https://github.com/WICG/scheduling-apisIdeally, we would have a rule
prefer-set-immediate
which would prefer it oversetTimeout(..., 0)
, butsetImmediate
doesn’t work in the browser, so it’s not a good API to use.