From f313e8bd6165d36c5f24fcdf676581ef2dbe050f 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 --- 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 35ebb39115f3be..7cd40f96cc9812 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -125,7 +125,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() {}); + } +);