Promises
Promises copied to clipboard
Failures get swallowed and lost.
When I debug the example below I see that wrap() swallows the exception even in the case that no rejection handlers are registered. This is not a good thing: consider that the function that throws here could be many libraries away and know nothing about Promises.
var testResult = (function () {
var p = new Promise(function(r) {
setTimeout(function() {
r.resolve();
}, 10);
});
p.then(function() {
throw new Error('blam');
});
return p;
})();
testResult.then(function(value) {
console.log("success");
}, function(error) {
console.error(error);
});
am i right that p
is not rejected - it's var p2 = p.then( ... )
which is rejected and testResult
is p
not p2
?
testResult is certainly p
. Something is rejected but I have no way to know if it is the return value of p.then()
.
That p.then()
is an internally created Promise helps explain this bug from a naive dev's point of view: if the system is creating promises and I am not resolving them it can't be a good result.
This issue is discussed on http://documentup.com/kriskowal/q/ where they recommend using the .done() function to ensure that exceptions are reported.