简体   繁体   English

如何阻止执行直到执行异步调用?

[英]how to block execution until Asynchronous call is executed?

I am making an asynchronous call using plain java script whose result is to be used further. 我正在使用纯Java脚本进行异步调用,其结果将被进一步使用。 Here are the statements. 以下是声明。

var x = function call();   //Asynchronous call
   alert(x);

here I am getting x as "undefined" . 在这里,我得到x为“ undefined”。

then I tried to check the status of x using While loop as below 然后我尝试使用While循环检查x的状态,如下所示

while(1){
  if(x != "undefined"){
       alert(x);
       break;
  }
}

Then the while condition is never getting a break. 然后,while条件永远不会中断。 It goes on execution. 它继续执行。 Help me out to hoe to stop the execution until i receive result from Asynchronous call? 帮助我急于停止执行,直到收到异步调用的结果?

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM