Browse Source

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 <d@domenic.me>
Reviewed-By: Petka Antonov <petka_antonov@hotmail.com>
v2.3.1-release
Vladimir Kurchatkin 10 years ago
parent
commit
a251657058
  1. 8
      src/node.js
  2. 15
      test/parallel/test-promises-unhandled-rejections.js

8
src/node.js

@ -515,8 +515,12 @@
var hasBeenNotified = hasBeenNotifiedProperty.get(promise);
if (hasBeenNotified !== undefined) {
hasBeenNotifiedProperty.delete(promise);
if (hasBeenNotified === true)
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);
});
else
NativeModule.require('assert').fail('unexpected PromiseRejectEvent');
});

15
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' +

Loading…
Cancel
Save