Skip to content

Commit

Permalink
node: mark promises as handled as soon as possible
Browse files Browse the repository at this point in the history
Fixes: #1912
PR-URL: #1952
Reviewed-By: Domenic Denicola <[email protected]>
Reviewed-By: Petka Antonov <[email protected]>
  • Loading branch information
vkurchatkin committed Jun 12, 2015
1 parent 03ce84d commit a251657
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,18 +515,20 @@
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);
});
}

}
}

process._setupPromises(function(event, promise, reason) {
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');
});
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-promises-unhandled-rejections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' +
Expand Down

0 comments on commit a251657

Please sign in to comment.