mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
internalBinding is used so often that it should just automatically be available for usage in internals. PR-URL: https://github.com/nodejs/node/pull/23025 Refs: https://github.com/nodejs/node/commit/2a9eb31 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
32 lines
878 B
JavaScript
32 lines
878 B
JavaScript
'use strict';
|
|
|
|
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
|
|
const { AsyncResource } = require('async_hooks');
|
|
const { getDefaultTriggerAsyncId } = require('internal/async_hooks');
|
|
const { enqueueMicrotask } = internalBinding('util');
|
|
|
|
// declared separately for name, arrow function to prevent construction
|
|
const queueMicrotask = (callback) => {
|
|
if (typeof callback !== 'function') {
|
|
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
|
|
}
|
|
|
|
const asyncResource = new AsyncResource('Microtask', {
|
|
triggerAsyncId: getDefaultTriggerAsyncId(),
|
|
requireManualDestroy: true,
|
|
});
|
|
|
|
enqueueMicrotask(() => {
|
|
asyncResource.runInAsyncScope(() => {
|
|
try {
|
|
callback();
|
|
} catch (e) {
|
|
process.emit('error', e);
|
|
}
|
|
});
|
|
asyncResource.emitDestroy();
|
|
});
|
|
};
|
|
|
|
module.exports = { queueMicrotask };
|