In my java script there is one function which return status(Available, Not Available, etc).
var status;
var cookieItem=getFromCookie('abc');
switch(cookieItem){
case '0':
status='Available';
break;
case '1':
status='Not Available';
break;
default:
status=_getFromServer();
break;
}
return status;
function() _getFromServer(){
$.ajax({
type: 'GET',
url: apiUrl,
contentType: 'application/json',
success: function (response) {
return 'Available';
},
error: function (xhr) {
return 'Not Available';
}
});
}
Here, _getFromServer() is async. So if user gets status from cookie is's fine but if there is no cookie available then it will return 'undefined' as _getFromServer is async.
I don't know how can I achieve my requirements.
I search on internet net and I found this question of SO, but I don't know how can it fulfill my requirements.