diff --git a/lib/internal/process/task_queues.js b/lib/internal/process/task_queues.js index 3cf07601a23aa4..76147473b04f9b 100644 --- a/lib/internal/process/task_queues.js +++ b/lib/internal/process/task_queues.js @@ -39,6 +39,8 @@ const { } = require('internal/errors').codes; const FixedQueue = require('internal/fixed_queue'); +const { AsyncResource } = require('async_hooks'); + // *Must* match Environment::TickInfo::Fields in src/env.h. const kHasTickScheduled = 0; @@ -132,16 +134,6 @@ function nextTick(callback) { queue.push(tickObject); } -let AsyncResource; -const defaultMicrotaskResourceOpts = { requireManualDestroy: true }; -function createMicrotaskResource() { - // Lazy load the async_hooks module - if (AsyncResource === undefined) { - AsyncResource = require('async_hooks').AsyncResource; - } - return new AsyncResource('Microtask', defaultMicrotaskResourceOpts); -} - function runMicrotask() { this.runInAsyncScope(() => { const callback = this.callback; @@ -153,12 +145,17 @@ function runMicrotask() { }); } +const defaultMicrotaskResourceOpts = { requireManualDestroy: true }; + function queueMicrotask(callback) { if (typeof callback !== 'function') { throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback); } - const asyncResource = createMicrotaskResource(); + const asyncResource = new AsyncResource( + 'Microtask', + defaultMicrotaskResourceOpts + ); asyncResource.callback = callback; enqueueMicrotask(FunctionPrototypeBind(runMicrotask, asyncResource)); diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index c751e41dcca6da..36d296ec0aa415 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -31,6 +31,7 @@ const expectedModules = new Set([ 'Internal Binding types', 'Internal Binding url', 'Internal Binding util', + 'NativeModule async_hooks', 'NativeModule buffer', 'NativeModule events', 'NativeModule fs',