I wrote this code below, it's pretty straightforward and what it dose is make a xhr request and then assign the response to a variable.
function myFunction(url){
var myVariable;
var myRequest = dojo.xhrGet({
url: url,
handleAs: "json"
}).then(requestSucceeded, requestFailed);
function requestSucceeded(response){
myVariable = response;
console.log('Value from the original response: ' + myVariable);
}
function requestFailed(error){
console.log("Error: ", error.message);
}
console.log('Value that will be returned to browser: ' + myVariable);
return myVariable;
}
It works fine in Chrome, Firefox and IE 10, and I can see two messages from the console:
Value from the original response: some value
Value that will be returned to browser: some value
But issue comes when I run it in IE 9 or 8, it won't assign the response two the variable outside of the xhr object, I see two messages from the console like:
Value that will be returned to browser: undefined
Value from the original response: some value
I know it's caused by the non-blocking nature of JavaScript so the code keeps moving along even the response has not come to the callback yet, which made myVariable at the end of the code as undefined.
But what I can't understand is why it's working fine in Chrome and Firefox, why would Chrome wait till the response is received and then move along to execute rest of code.
So my question is how can I make it work in IE 9?
Thanks a lot!