From 5bca4c7cc0c7e712d70347fa84f8b015f0c5fadd Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sat, 24 Nov 2018 12:15:03 -0800 Subject: [PATCH] process: emit unhandled warning immediately PR-URL: https://github.com/nodejs/node/pull/24632 Fixes: https://github.com/nodejs/node/issues/24209 Reviewed-By: Gus Caplan Reviewed-By: Benjamin Gruenbaum Reviewed-By: Franziska Hinkelmann --- lib/internal/process/promises.js | 7 +++---- .../test-promises-unhandled-rejections.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index d70deb516025fe..31c96293b341b9 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -108,7 +108,7 @@ function emitPromiseRejectionWarnings() { } } - let hadListeners = false; + let maybeScheduledTicks = false; let len = pendingUnhandledRejections.length; while (len--) { const promise = pendingUnhandledRejections.shift(); @@ -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; } diff --git a/test/parallel/test-promises-unhandled-rejections.js b/test/parallel/test-promises-unhandled-rejections.js index fdbf17b9594656..4464fad8e4b846 100644 --- a/test/parallel/test-promises-unhandled-rejections.js +++ b/test/parallel/test-promises-unhandled-rejections.js @@ -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); + }, +);