cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 394 forks source link

call the catch handler only if it is a function #506

Closed marijaselakovic closed 5 months ago

marijaselakovic commented 6 years ago

Hello,

You should include the type check on catch handler because if a handler is not a function, the catch method throws the TypeError

TypeError: handler.call is not a function

For example, check the following code:

var p1 = Promise.resolve(18);
var p2 = Promise.reject(17);
p2.catch(function(){
return p1;
}, p1);

The second argument is non-function value and in this case the TypeError is thrown becuase method call can't be called on a non-function object.

unscriptable commented 6 years ago

Throwing a TypeError seems more informative than rejecting with false to me. There have been several (dozens and dozens) of conversations about throwing sync Errors versus returning (async) rejections. Most of these discussions have concluded (iirc) that coding errors (rather than runtime errors) should throw sync.

Providing a non-function as a handler parameter seems like a coding error, doesn't it?

Except, perhaps, in very generic code, but this is an edge case.