I want to make a function that supports done
but without returning ajax.
function checkAndReturn(){
if(global.id){
return global.id; // the problem might be here
}else{
return $.get("http://someurl.com",null,function(){...})
}
}
Here checkAndReturn.done(function(){...})
works if global.id
is not available, but it is not working if global.id
is available.
I think I should be returning any other object and box my data to that object to make my done work, maybe xhr
?.
You should return a (resolved) jQuery Deferred
object. The jqXHR
object returned by jQuery's AJAX functions is derived from Deferred
as well, so the "interface" of your function will be clean - your function will return a Deferred
in any case.
You could do something like this (just an example, adapt it to your use case):
function checkAndReturn(){
if (global.id){
return $.Deferred().resolve(global.id);
}
else {
return $.get("http://someurl.com", null, function(){...})
}
}