From 5eafe3b5cb321dd4cb25be00490175ee6a2a5322 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sat, 14 Dec 2019 13:53:14 +0800 Subject: [PATCH] process: fix promise catching Fixes: https://github.com/nodejs/node/issues/30953 PR-URL: https://github.com/nodejs/node/pull/30957 Reviewed-By: Anna Henningsen Reviewed-By: Anatoli Papirovski Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: David Carlier Reviewed-By: Rich Trott Reviewed-By: Gus Caplan Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- lib/internal/process/promises.js | 3 ++- test/parallel/test-promises-unhandled-rejections.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index ab833776070449..47d58de99611fe 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -123,7 +123,8 @@ function handledRejection(promise) { return; } } - setHasRejectionToWarn(false); + if (maybeUnhandledPromises.size === 0 && asyncHandledRejections.length === 0) + setHasRejectionToWarn(false); } const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning'; diff --git a/test/parallel/test-promises-unhandled-rejections.js b/test/parallel/test-promises-unhandled-rejections.js index 176bae8a73ed1a..f4294800fa9dab 100644 --- a/test/parallel/test-promises-unhandled-rejections.js +++ b/test/parallel/test-promises-unhandled-rejections.js @@ -718,3 +718,15 @@ asyncTest( let timer = setTimeout(common.mustNotCall(), 10000); }, ); + +// https://github.com/nodejs/node/issues/30953 +asyncTest( + 'Catching a promise should not take effect on previous promises', + function(done) { + onUnhandledSucceed(done, function(reason, promise) { + assert.strictEqual(reason, '1'); + }); + Promise.reject('1'); + Promise.reject('2').catch(function() {}); + } +);