Promises icon indicating copy to clipboard operation
Promises copied to clipboard

Failures get swallowed and lost.

Open johnjbarton opened this issue 11 years ago • 3 comments

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);
});

johnjbarton avatar Oct 01 '13 01:10 johnjbarton

am i right that p is not rejected - it's var p2 = p.then( ... ) which is rejected and testResult is p not p2?

neonstalwart avatar Oct 01 '13 02:10 neonstalwart

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.

johnjbarton avatar Oct 01 '13 15:10 johnjbarton

This issue is discussed on http://documentup.com/kriskowal/q/ where they recommend using the .done() function to ensure that exceptions are reported.

johnjbarton avatar Oct 02 '13 01:10 johnjbarton