I have the following function
function getDataAvailablity() {
var isDataAvailable;
$.ajax({
url: 'http://someurl/data.json',
async: false,
dataType: json
}).success(function() {
isDataAvailable = true;
}).failure(function() {
isDataAvailable = false;
});
return isDataAvaialble;
}
Is it possible to replace the code within the getDataAvailability() to work without async false ajax call.
Also function signature of getDataAvailablity should not change as already this function is used in many places, so its difficult to change at all the places where this API is being used.
it is preferred to not have jQuery. We can use AngularJS or native javascript.
the solution provided in various other threads requires either change of the signature of the function. If i use callback then i need to ask my consumers to pass a callback function as argument.
and if i use a deferred object or promise then consumers needs to change there code to use .then function in addition to getDataAvailability().
this funciton should return a boolean and takes no argument and should be consumable just by calling
var flag = getDataAvailability()
Please also let me know incase you think this is not possible.
Solution I am currently using requires changing function signature and is as below:
// API
function getDataAvailablity() {
var deferred = $q.defer();
$http.get('http://someurl/data.json');
return deferred.promise();
}
// calling this API
getDataAvailability().then(success, error);
function success(){
// on success
}
funciton error() {
// on error
}
Please provide some approach which do not require changing function signature. and comment if such a solution is possible or not.