It doesn't seem like I can return a deferred object if it failed. I have a pretty nested ajax request that goes into a queue so I need a way to return the request as a deferred object. Everything is great if the request is successful, but I am stuck on getting failures to propagate.
https://jsfiddle.net/3wtady9r/
function doAjax(file) {
var defer = $.Deferred();
var fakeFile = file;
var data = null;
ajax();
function ajax() {
return $.get(fakeFile)
.done(function(data) {
data = data;
defer.resolve(data);
})
.fail(function() {
defer.fail();
})
}
return defer.promise(data);
}
var thisWillWork = '';
doAjax(thisWillWork)
.done(function() {
console.log('done')
})
.fail(function() {
console.error('fail')
})
.always(function() {
console.log('always')
})
var thisWontWork = 'fakeFile.html'
doAjax(thisWontWork)
.done(function() {
console.log('done')
})
.fail(function() {
console.error('fail')
})
.always(function() {
console.log('always')
})
When I do doAjax(thisWontWork) I am expecting to get the console error and also .always, but neither fire.