I'm using jQuery for AJAX but Q elsewhere in our application so want to ensure that the Promise implementation is consistent.
I've wrapped the jQuery AJAX call with Q like so:
Q($.ajax(url, {
type: 'get'
}));
And it works all fine for success responses. Now I want to add error handling, I want to have global error handling (since we have a consistent error response from our server), making an API like so:
var xhr = function (url) {
return Q($.ajax(url, {
type: 'get'
}))
.then(function (data) {
return data;
}, function (res) {
//global error handling
});
};
And I'd expect to use it like this:
xhr('...').then(function () { console.log('success'); });
The problem is when the global error handler runs the "success" method on the consumer of the xhr
method is invoked.
How would you prevent that?
A runnable sample can be seen here - http://jsbin.com/gudifu/3/edit