Skip to content

Commit

Permalink
process: emit unhandled warning immediately
Browse files Browse the repository at this point in the history
PR-URL: #24632
Fixes: #24209
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
  • Loading branch information
apapirovski authored and Trott committed Nov 28, 2018
1 parent b5c5d20 commit 3ce9305
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function emitPromiseRejectionWarnings() {
}
}

let hadListeners = false;
let maybeScheduledTicks = false;
let len = pendingUnhandledRejections.length;
while (len--) {
const promise = pendingUnhandledRejections.shift();
Expand All @@ -118,10 +118,9 @@ function emitPromiseRejectionWarnings() {
const { reason, uid } = promiseInfo;
if (!process.emit('unhandledRejection', reason, promise)) {
emitWarning(uid, reason);
} else {
hadListeners = true;
}
maybeScheduledTicks = true;
}
}
return hadListeners || pendingUnhandledRejections.length !== 0;
return maybeScheduledTicks || pendingUnhandledRejections.length !== 0;
}
19 changes: 19 additions & 0 deletions test/parallel/test-promises-unhandled-rejections.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,3 +699,22 @@ asyncTest('Rejected promise inside unhandledRejection allows nextTick loop' +
process.nextTick(() => promise.catch(() => done()));
});
});

asyncTest(
'Unhandled promise rejection emits a warning immediately',
function(done) {
clean();
Promise.reject(0);
const { emitWarning } = process;
process.emitWarning = common.mustCall((...args) => {
if (timer) {
clearTimeout(timer);
timer = null;
done();
}
emitWarning(...args);
}, 2);

let timer = setTimeout(common.mustNotCall(), 10000);
},
);

0 comments on commit 3ce9305

Please sign in to comment.