Skip to content

Commit 17de9ba

Browse files
committed
lib: un microtasks before ticks
This resolve multiple timing issues related to promises and nextTick. As well as resolving zaldo in promise only code, i.e. our current best practice of using process.nextTick will always apply and work. Refs: nodejs#51156 Refs: nodejs#51156 (comment) Refs: nodejs#51114 Refs: nodejs#51070 Refs: nodejs#51156 PR-URL: nodejs#51267
1 parent fc102f2 commit 17de9ba

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/internal/process/task_queues.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ const {
3434
symbols: { async_id_symbol, trigger_async_id_symbol },
3535
} = require('internal/async_hooks');
3636
const FixedQueue = require('internal/fixed_queue');
37+
const { getOptionValue } = require('internal/options');
3738

3839
const {
3940
validateFunction,
4041
} = require('internal/validators');
4142

43+
const experimentalTaskOrdering = getOptionValue('--experimental-task-ordering');
44+
4245
const { AsyncResource } = require('async_hooks');
4346

4447
// *Must* match Environment::TickInfo::Fields in src/env.h.
@@ -67,6 +70,9 @@ function runNextTicks() {
6770
function processTicksAndRejections() {
6871
let tock;
6972
do {
73+
if (experimentalTaskOrdering) {
74+
runMicrotasks();
75+
}
7076
while ((tock = queue.shift()) !== null) {
7177
const asyncId = tock[async_id_symbol];
7278
emitBefore(asyncId, tock[trigger_async_id_symbol], tock);
@@ -92,7 +98,9 @@ function processTicksAndRejections() {
9298

9399
emitAfter(asyncId);
94100
}
95-
runMicrotasks();
101+
if (!experimentalTaskOrdering) {
102+
runMicrotasks();
103+
}
96104
} while (!queue.isEmpty() || processPromiseRejections());
97105
setHasTickScheduled(false);
98106
setHasRejectionToWarn(false);

0 commit comments

Comments
 (0)