How do I do a blocking call in javascript?
In the code below the alert at the last line of this code snippet hits before the code in if block completes which returns some value, by calling chached.get method in different js file.
var getCachedData = function(_key){
var retStr;
if (_key != '' || _key != null) {
cached.get(_key, function(data){
if(data){
alert('data = ' +data.value);
return data.value;
}
else{
return undefine;
}
});
}
alert('completed');
}
The
cached.getmethod must be doing something asynchronously. The function you supply to that method as an argument appears to be a callback function that executes once thegetmethod has returned successfully. If you need to run code that is dependant on the result of the call tocached.get, you will need to put it inside that callback.