Skip to content

Commit

Permalink
use queueMicrotask instead of setTimeout in browsers in order to avoi…
Browse files Browse the repository at this point in the history
…d browser throttling (#1761)
  • Loading branch information
jensengar committed Aug 5, 2021
1 parent c243b0e commit 89255fe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/internal/setImmediate.js
@@ -1,6 +1,7 @@
'use strict';
/* istanbul ignore file */

export var hasQueueMicrotask = typeof queueMicrotask === 'function' && queueMicrotask;
export var hasSetImmediate = typeof setImmediate === 'function' && setImmediate;
export var hasNextTick = typeof process === 'object' && typeof process.nextTick === 'function';

Expand All @@ -14,7 +15,9 @@ export function wrap(defer) {

var _defer;

if (hasSetImmediate) {
if (hasQueueMicrotask) {
_defer = queueMicrotask;
} else if (hasSetImmediate) {
_defer = setImmediate;
} else if (hasNextTick) {
_defer = process.nextTick;
Expand Down
6 changes: 3 additions & 3 deletions test/cargoQueue.js
Expand Up @@ -76,12 +76,12 @@ describe('cargoQueue', () => {
var call_order = [];
var c = async.cargoQueue(worker.bind({ call_order }), 2, 2);
c.push(1);
setImmediate(() => {
async.setImmediate(() => {
c.push(2);
setImmediate(() => {
async.setImmediate(() => {
c.push(3);
c.push(4);
setImmediate(() => {
async.setImmediate(() => {
c.push(5);
c.drain(() => {
expect(call_order).to.eql([
Expand Down

0 comments on commit 89255fe

Please sign in to comment.