From a2516570589db22ffa142644b0e633f22b293c32 Mon Sep 17 00:00:00 2001 From: Vladimir Kurchatkin Date: Thu, 11 Jun 2015 23:08:17 +0300 Subject: [PATCH] node: mark promises as handled as soon as possible Fixes: https://github.com/nodejs/io.js/issues/1912 PR-URL: https://github.com/nodejs/io.js/pull/1952 Reviewed-By: Domenic Denicola Reviewed-By: Petka Antonov --- src/node.js | 12 +++++++----- .../test-promises-unhandled-rejections.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/node.js b/src/node.js index 25164784cfebbe..292c7b22133ee6 100644 --- a/src/node.js +++ b/src/node.js @@ -515,8 +515,12 @@ var hasBeenNotified = hasBeenNotifiedProperty.get(promise); if (hasBeenNotified !== undefined) { hasBeenNotifiedProperty.delete(promise); - if (hasBeenNotified === true) - process.emit('rejectionHandled', promise); + if (hasBeenNotified === true) { + process.nextTick(function() { + process.emit('rejectionHandled', promise); + }); + } + } } @@ -524,9 +528,7 @@ if (event === promiseRejectEvent.unhandled) unhandledRejection(promise, reason); else if (event === promiseRejectEvent.handled) - process.nextTick(function() { - rejectionHandled(promise); - }); + rejectionHandled(promise); else NativeModule.require('assert').fail('unexpected PromiseRejectEvent'); }); diff --git a/test/parallel/test-promises-unhandled-rejections.js b/test/parallel/test-promises-unhandled-rejections.js index 9a186de8dfec79..e5b3e6f35c0d45 100644 --- a/test/parallel/test-promises-unhandled-rejections.js +++ b/test/parallel/test-promises-unhandled-rejections.js @@ -275,6 +275,21 @@ asyncTest('Attaching a promise catch in a process.nextTick is soon enough to' + }); }); +asyncTest('While inside setImmediate, catching a rejected promise derived ' + + 'from returning a rejected promise in a fulfillment handler ' + + 'prevents unhandledRejection', function(done) { + onUnhandledFail(done); + + setImmediate(function() { + // reproduces on first tick and inside of setImmediate + Promise + .resolve('resolve') + .then(function() { + return Promise.reject('reject'); + }).catch(function(e) {}); + }); +}); + // State adapation tests asyncTest('catching a promise which is asynchronously rejected (via' + 'resolution to an asynchronously-rejected promise) prevents' +