wookiehangover / underscore.deferred

jQuery style Deferreds for Underscore
https://github.com/wookiehangover/underscore.deferred
MIT License
225 stars 21 forks source link

.then is swallowing exceptions #16

Open danheberden opened 11 years ago

danheberden commented 11 years ago

When using .then - an exception thrown (such as a ReferenceError) gets swallowed up making debugging near impossible.

var Deferred = require( 'underscore.deferred' );

// using then won't throw any exceptions
var d1 = Deferred.Deferred();
d1.then( function( a ) {
  console.log( 'success', a );
  missing_var.destruction;
});
d1.resolve( 'test' );

// output:
// => success test

// using done/fail will
var d2 = Deferred.Deferred();
d2.done( function( a ) {
  console.log( 'success', a );
  missing_var.destruction;
});
d2.resolve( 'test' );

// output: 
// => success test
//   /tmp/deferred_bug/server.js:19
//    missing_var.destruction;
//    ^
//  ReferenceError: missing_var is not defined
//     at Object.<anonymous> (/private/tmp/deffered_Bug/wat.js:19:3)
//     at _d.Callbacks.fire (/private/tmp/deffered_Bug/node_modules/underscore.deferred/underscore.deferred.js:149:36)
//     at Object._d.Callbacks.self.fireWith (/private/tmp/deffered_Bug/node_modules/underscore.deferred/underscore.deferred.js:259:15)
//     at Object._d.Callbacks.self.fire [as resolve] (/private/tmp/deffered_Bug/node_modules/underscore.deferred/underscore.deferred.js:266:16)
//     at Object.<anonymous> (/private/tmp/deffered_Bug/wat.js:21:4)
//     at Module._compile (module.js:449:26)
//     at Object.Module._extensions..js (module.js:467:10)
//     at Module.load (module.js:356:32)
//     at Function.Module._load (module.js:312:12)
//     at Module.runMain (module.js:492:10)

I assume this has to do with the changes to .then to handle exception throwing - which sounds great but makes this kind of stuff a huge pain in the ass. For example, jQuery handle the exception fine: http://jsfiddle.net/danheberden/G5RFD/1/

wookiehangover commented 11 years ago

@danheberden sorry for not getting to this in December when you opened it. I "went dark" that week and missed the original email.

I'm not really sure how to proceed in this one, since in the Promises/A spec says

If the callback throws an error, the returned promise will be moved to failed state.

BUT I'd be willing to alter my implementation to something that makes more sense than swallowing the error...

gnarf commented 11 years ago

This is my biggest complaint about the Promises/A spec.

urbien commented 10 years ago

can you point me to a place in the code to change to make this aspect jquery compatible