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 93ac186a3b45f6..02f84b674a089a 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); + }, +);