I am making an asynchronous call using plain java script whose result is to be used further. Here are the statements.
var x = function call(); //Asynchronous call
alert(x);
here I am getting x as "undefined" .
then I tried to check the status of x using While loop as below
while(1){
if(x != "undefined"){
alert(x);
break;
}
}
Then the while condition is never getting a break. It goes on execution. Help me out to hoe to stop the execution until i receive result from Asynchronous call?
asked Nov 26, 2013 at 12:40
Karthik Dheeraj
1,0491 gold badge14 silver badges26 bronze badges
1 Answer 1
Asynchronous call returns the output once it processed. So your function should accept a callback which indeed runs when the function is executed.
SO your code should look like this
function asynccall (args..., callback);
function callback(data){
alert(data); //Get the response and use it furher
}
Sign up to request clarification or add additional context in comments.
Comments
lang-js
xwill never change once your function call has returned. Instead, you will likely be able to pass a callback function somewhere in your asynchronous call, and this function will be called when the async request is complete. We can't really help you until you post the code for the asynchronous function...