An example to make clear what i want to do. This is what I would usually do:
function success(data, status, jqxhr){
if ( data.error )
return failure(jqxhr, status, data.error);
// process data
}
function failure(jqxhr, status, err){
...
}
$.ajax( ... )
.done(success)
.fail(failure)
Is there any way, i can accomplish this with anonymous functions only, like so?
$.ajax( ... )
.done(function(data, status, jqxhr){
if(data.error)
// what do i need to do here to jump in to the fail handler?
})
.fail(function(jqxhr, status, err){
...
})
Don't use
done
, butthen
to map over the result of the promise. By returning a rejected promise you can "throw" an error and reject the resulting promise, so that your fail handler is executed.