Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node: mark promises as handled as soon as possible #1952

Merged
merged 1 commit into from
Jun 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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