Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
process: stop rejection loop early
This adds a fast path to the unhandledRejections and handledRejections
to actually stop trying to emit something if no hook is attached.
  • Loading branch information
BridgeAR committed Sep 5, 2018
commit 46b0c97bca7a25f491552b77116aecb8e0dfc83a
15 changes: 13 additions & 2 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { ERR_INVALID_ENV_VALUE } = require('internal/errors').codes;
const { safeToString } = process.binding('util');

const maybeUnhandledPromises = new WeakMap();
const pendingUnhandledRejections = [];
const asyncHandledRejections = [];
let pendingUnhandledRejections = [];
let asyncHandledRejections = [];
const kFromPromise = true;
let lastPromiseId = 0;

Expand Down Expand Up @@ -105,6 +105,10 @@ function emitPromiseRejectionWarnings() {
const { promise, warning } = asyncHandledRejections.shift();
if (!process.emit('rejectionHandled', promise)) {
process.emitWarning(warning);
} else {
// Stop the loop early.
asyncHandledRejections = [];
break;
}
}
}
Expand Down Expand Up @@ -141,6 +145,13 @@ function emitPromiseRejectionWarnings() {
} else if (!process.emit('unhandledRejection', reason, promise)) {
if (state === kWarn || state === kErrorOnGC) {
emitWarning(uid, reason);
// Stop the loop as soon as possible. Since the handler might have been
// attached in the beginning and then removed again and a new unhandled
// rejection might have been added at that former state, only stop if no
// new rejections were added.
} else if (len === pendingUnhandledRejections.length) {
pendingUnhandledRejections = [];
break;
}
} else {
hadListeners = true;
Expand Down