@@ -113,24 +113,6 @@ if (isMainThread) {
113
113
process . exit = wrapped . exit ;
114
114
}
115
115
116
- const {
117
- emitWarning
118
- } = require ( 'internal/process/warning' ) ;
119
-
120
- process . emitWarning = emitWarning ;
121
-
122
- const {
123
- nextTick,
124
- runNextTicks
125
- } = require ( 'internal/process/task_queues' ) . setupTaskQueue ( ) ;
126
-
127
- process . nextTick = nextTick ;
128
- // Used to emulate a tick manually in the JS land.
129
- // A better name for this function would be `runNextTicks` but
130
- // it has been exposed to the process object so we keep this legacy name
131
- // TODO(joyeecheung): either remove it or make it public
132
- process . _tickCallback = runNextTicks ;
133
-
134
116
const credentials = internalBinding ( 'credentials' ) ;
135
117
if ( credentials . implementsPosixCredentials ) {
136
118
process . getuid = credentials . getuid ;
@@ -186,6 +168,11 @@ if (config.hasInspector) {
186
168
internalBinding ( 'inspector' ) . registerAsyncHook ( enable , disable ) ;
187
169
}
188
170
171
+ const {
172
+ setupTaskQueue,
173
+ queueMicrotask
174
+ } = require ( 'internal/process/task_queues' ) ;
175
+
189
176
if ( ! config . noBrowserGlobals ) {
190
177
// Override global console from the one provided by the VM
191
178
// to the one implemented by Node.js
@@ -317,6 +304,32 @@ Object.defineProperty(process, 'features', {
317
304
hasUncaughtExceptionCaptureCallback ;
318
305
}
319
306
307
+ const { emitWarning } = require ( 'internal/process/warning' ) ;
308
+ process . emitWarning = emitWarning ;
309
+
310
+ // We initialize the tick callbacks and the timer callbacks last during
311
+ // bootstrap to make sure that any operation done before this are synchronous.
312
+ // If any ticks or timers are scheduled before this they are unlikely to work.
313
+ {
314
+ const { nextTick, runNextTicks } = setupTaskQueue ( ) ;
315
+ process . nextTick = nextTick ;
316
+ // Used to emulate a tick manually in the JS land.
317
+ // A better name for this function would be `runNextTicks` but
318
+ // it has been exposed to the process object so we keep this legacy name
319
+ // TODO(joyeecheung): either remove it or make it public
320
+ process . _tickCallback = runNextTicks ;
321
+
322
+ const { getTimerCallbacks } = require ( 'internal/timers' ) ;
323
+ const { setupTimers } = internalBinding ( 'timers' ) ;
324
+ const { processImmediate, processTimers } = getTimerCallbacks ( runNextTicks ) ;
325
+ // Sets two per-Environment callbacks that will be run from libuv:
326
+ // - processImmediate will be run in the callback of the per-Environment
327
+ // check handle.
328
+ // - processTimers will be run in the callback of the per-Environment timer.
329
+ setupTimers ( processImmediate , processTimers ) ;
330
+ // Note: only after this point are the timers effective
331
+ }
332
+
320
333
function setupProcessObject ( ) {
321
334
const EventEmitter = require ( 'events' ) ;
322
335
const origProcProto = Object . getPrototypeOf ( process ) ;
@@ -430,7 +443,6 @@ function setupQueueMicrotask() {
430
443
get ( ) {
431
444
process . emitWarning ( 'queueMicrotask() is experimental.' ,
432
445
'ExperimentalWarning' ) ;
433
- const { queueMicrotask } = require ( 'internal/process/task_queues' ) ;
434
446
435
447
Object . defineProperty ( global , 'queueMicrotask' , {
436
448
value : queueMicrotask ,
0 commit comments