I'm using JQuery's $.when
and $.get
to fetch some data and do something with it. When there is an error fetching it or doing something with it, I want to handle that error, and handle it differently depending on which data I was fetching/at which path I was fetching it.
Unfortunately, the information passed to $.when().fail(...)
is scant, and doesn't include that information. How might I bubble up that information from the deferrable in a non-hacky[1] way?
$.when(get(path1), get(path2)
.done(function(parsed1, parsed2) {
// do something with this date
}).fail(function(jqXHR, statusTxt, errTxt) {
// do something depending on which path failed...
});
function get(path) {
var parser = U.parser();
return $.get('/getter' + path)
.then(function(data) {
return parser(data);
});
}
[1]: Sure, I could close around the path and some global objects I want to handle failure with, and handle it in a .fail()
on the Deferred that $.get
returns. I could also pass up a Deferred that doesn't failed, but wraps the failure in some way, but that also feel very hacky.
Since the jqXHR object is one of the things passed back to
$.when()
it seems you can put things in there related to your request. For example, if the path is something you want, you could do this:Then, in your
$.when()
handler, you will have the jqXHR object that you can extract parameters from.Note,
$.when()
rejects as soon as any single request rejects. If you actually want all requests to continue and you want to know when all have been fullfilled (whether resolved or rejected), you can't use$.when()
for that. jQuery doesn't natively offer that function. You can either build it yourself or use an external promise library that offers that (I use Bluebird which hasPromise.settle()
.Here's a working example:
Working demo: http://jsfiddle.net/jfriend00/g8z353cz/
Here's another approach that is fully supported in the promise world. This takes over the resolution of the ajax promise so that you can put exactly whatever data you want in it. In this particular implementation, I chose to always resolve and just use the data in the resolved value to tell you which ajax calls succeeded or failed, but you could use rejection with a value too if you wanted. That is guaranteed to be propagated all the way back:
And, you can see this sort of implementation here: http://jsfiddle.net/jfriend00/c0u40gqh/
Try
Note, stops on first
error
, though can implement workaround for this. Below primarily for possible custom error patternjsfiddle http://jsfiddle.net/guest271314/o3Lk73wg/