flush-microtasks
Version:
Flush the microtask queue
36 lines • 1.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var enqueueTask;
try {
// read require off the module object to get around the bundlers.
// we don't want them to detect a require and bundle a Node polyfill.
var requireString = ("require" + Math.random()).slice(0, 7);
var nodeRequire = module && module[requireString];
// assuming we're in node, let's try to get node's
// version of setImmediate, bypassing fake timers if any.
enqueueTask = nodeRequire('timers').setImmediate;
}
catch (_err) {
// we're in a browser
// we can't use regular timers because they may still be faked
// so we try MessageChannel+postMessage instead
enqueueTask = function (callback) {
var supportsMessageChannel = typeof MessageChannel === 'function';
if (supportsMessageChannel) {
var channel = new MessageChannel();
channel.port1.onmessage = callback;
channel.port2.postMessage(undefined);
}
else {
throw Error('Cannot flush without the global MessageChannel class');
}
};
}
exports.flushMicroTasks = function () {
return ({
then: function (resolve) {
enqueueTask(resolve);
},
});
};
//# sourceMappingURL=index.js.map