Deferred Error Handling
Contents |
{ defer.resolve(); }); return defer.promise; } var state = 'before foo'; foo() .then(function onSuccess() { console.log('success!'); state = 'foo finished'; },
Q Promise
function onError() { console.log('error :('); }); process.on('exit', function () { console.log('on promise catch exit, state =', state); }); If everything goes well, the program prints (I am using node): $
Promise Chaining
node index.js success! on exit, state = foo finished two problems The obvious problem: if foo throws an error, or rejects the promise, the onError callback never sets npm q the correct state. var q = require('q'); function foo() { var defer = q.defer(); process.nextTick(function () { defer.reject(); }); return defer.promise; } var state = 'before foo'; foo() .then(function onSuccess() { console.log('success!'); state = 'foo finished'; }, function onError() { console.log('error :('); }); process.on('exit', function () { console.log('on exit, state =', state); }); // prints error :( q promise error handling on exit, state = before foo Less obvious problem: if onSuccess throws an error, the function onError is NOT called var q = require('q'); function foo() { var defer = q.defer(); process.nextTick(function () { defer.resolve(); }); return defer.promise; } var state = 'before foo'; foo() .then(function onSuccess() { console.log('success!'); throw new Error('unexpected error'); state = 'foo finished'; }, function onError() { console.log('error :('); }); process.on('exit', function () { console.log('on exit, state =', state); }); // prints success! on exit, state = before foo Lets split this problem into two components: setting the right final state and handling all errors. Setting the final state Q and its q-lite version used by AngularJs allow setting a callback to execute in any situation, whether the promise is resolved or rejected via .finally. var q = require('q'); function foo() { var defer = q.defer(); process.nextTick(function () { defer.reject(); }); return defer.promise; } var state = 'before foo'; foo() .then(function onSuccess() { console.log('success!'); throw new Error('unexpected error'); }, function onError() { console.log('erro
here for a quick overview of the site Help Center Detailed answers to any questions you might have
Jquery Deferred Error Handling
Meta Discuss the workings and policies of this site About Us promise error chain Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with
Javascript Promise
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just http://bahmutov.calepin.co/error-handling-in-promises.html like you, helping each other. Join them; it only takes a minute: Sign up Error Handling and Recovery with jQuery Deferred up vote 3 down vote favorite 1 I am using jQuery and am aware that this issue is because the jQuery.Deferred implementation is not Promises/A+ compiant. I do not want to use any other libraries to http://stackoverflow.com/questions/35614632/error-handling-and-recovery-with-jquery-deferred solve this. With that out of the way, is there a way to recover from a $.Deferred().fail() callback such that I am returned back to the success chain? This is possible with the multi-callback form of then() but so far I have not found a solution using .fail() then: asyncThatWillFail().then(function () { // useless callback }, function () { console.log("error"); return $.Deferred().resolve(); }).then(function () { console.log("continuing on success chain"); }); fail (does not work): asyncThatWillFail().fail(function () { console.log("error"); return $.Deferred().resolve(); }).then(function () { console.log("continuing on success chain"); }); In my case I only need to check for failure, set a flag, and continue with what I was doing. I simply don't need the parallel success handler in the "then" example. Here is a jsFiddle to further clarify what I mean. javascript jquery promise jquery-deferred share|improve this question edited Feb 24 at 22:49 asked Feb 24 at 22:37 thedarklord47 929420 add a comment| 1 Answer 1 active oldest votes up vote 2 down vote accepted No, you
Team Conduct Brand Guide Donate jQuery API Documentation Download API Documentation Blog Plugins Browser Support search Search https://api.jquery.com/category/deferred-object/ jQuery API Documentation Category: Deferred Object The Deferred object, http://phdesign.com.au/programming/mapping-errors-in-jquery-promises-deferred/ introduced in jQuery 1.5, is a chainable utility object created by calling the jQuery.Deferred() method. It can register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function. The error handling Deferred object is chainable, similar to the way a jQuery object is chainable, but it has its own methods. After creating a Deferred object, you can use any of the methods below by either chaining directly from the object creation or saving the object in a variable and invoking one deferred error handling or more methods on that variable. deferred.always() Add handlers to be called when the Deferred object is either resolved or rejected. deferred.done() Add handlers to be called when the Deferred object is resolved. deferred.fail() Add handlers to be called when the Deferred object is rejected. Also in: Deprecated > Deprecated 1.7 | Removed deferred.isRejected() Determine whether a Deferred object has been rejected. Also in: Deprecated > Deprecated 1.7 | Removed deferred.isResolved() Determine whether a Deferred object has been resolved. deferred.notify() Call the progressCallbacks on a Deferred object with the given args. deferred.notifyWith() Call the progressCallbacks on a Deferred object with the given context and args. Also in: Deprecated > Deprecated 1.8 deferred.pipe() Utility method to filter and/or chain Deferreds. deferred.progress() Add handlers to be called when the Deferred object generates progress notifications. deferred.promise() Return a Deferred's Promise object. deferred.reject() Reject a Deferred object and call any failCallb
For those familiar with JavaScript Promises, jQuery’s Deferred.fail() handling can act unexpectedly. Standard Promises implementations allow the first catch handler to deal with the error and then return to normal execution flow (see JavaScript Promises: There and back again). jQuery on the other hand appears to execute all fail handlers, in the order they were defined with no chance to recover normal flow. For an example, consider this code using Chrome’s built in Promises support: function asyncSucceed() { return $.Deferred(function(deferred) { setTimeout(function() { console.log('async1'); deferred.resolve(); }, 300); }).promise(); }; function asyncFail() { return $.Deferred(function(deferred) { setTimeout(function() { console.log