如何在 JavaScript 中阻止异步函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4345945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:00:28  来源:igfitidea点击:

How to block on asynchronous functions in JavaScript

javascriptasynchronous

提问by Ryan Li

I need to write a function in JavaScript, which returns a state from calling an asynchronous function. However, the caller only receives the value and no callback function is to be provided. I tried something like:

我需要用 JavaScript 编写一个函数,它通过调用异步函数返回一个状态。但是,调用者只接收值,不提供回调函数。我试过类似的东西:

function getState() {
    var ret = null;
    asyncCall("request",
        function() { ret = "foo"; } // callback
    );
    while (ret === null)
        ; // block on the asynchronous call
    return ret;
}

However, the loop is never going to end…

然而,循环永远不会结束......

Any ideas? Thank you.

有任何想法吗?谢谢你。

采纳答案by tomg

I think you're looking for StratifiedJS, http://stratifiedjs.orgIt allows you to "orchestrate" your async code exactly like you wrote, BEWARE that it "writes" like synchronous code, it will NOT block the rest of your application. You can use it anywhere by loading the apollo js library.

我认为您正在寻找 StratifiedJS,http://stratifiedjs.org它允许您完全按照您编写的方式“编排”您的异步代码,请注意它像同步代码一样“编写”,它不会阻止您的应用程序的其余部分. 您可以通过加载 apollo js 库在任何地方使用它。

This is how it would look like in Stratified JavaScript:

这是它在 Stratified JavaScript 中的样子:

function getState() {
  waitfor (var ret) {
    // block on the asynchronous call
    asyncCall("request", resume);
  }
  return ret;
}

of course there are modules/libraries that would just make it look like this: http://onilabs.com/modules#http

当然,有些模块/库可以让它看起来像这样:http: //onilabs.com/modules#http

function getState() {
  return http.get("request");
}

回答by Dave Black

Unless I misunderstood your question, you could look at jQuery's ajaxStop() event - http://api.jquery.com/ajaxstop. This blocks until all ajax calls have completed. This obviously requires that all of your async calls be done thru jQuery.

除非我误解了您的问题,否则您可以查看 jQuery 的 ajaxStop() 事件 - http://api.jquery.com/ajaxstop。这将阻塞,直到所有 ajax 调用完成。这显然要求所有异步调用都通过 jQuery 完成。

$(document).ajaxStop(function() {
    // do your "ajax dependent" stuff here
});

回答by ?ime Vidas

Why not just:

为什么不只是:

asyncCall("request", function() {
    // here you can inspect the state
});

What's the point of the wrapper function?

包装函数的意义是什么?

Asynchronous functions work this way. If you want to block the execution, then use a synchronous call:

异步函数以这种方式工作。如果要阻止执行,则使用同步调用:

var state = syncCall("request");

回答by RoToRa

The best would be to put the logic you want to have called into the callback function. Is there any reason why you can't do that?

最好的方法是将您想要调用的逻辑放入回调函数中。你有什么理由不能这样做吗?

You could use setIntervalto check on the result, but that requires a callback function, too...

你可以setInterval用来检查结果,但这也需要一个回调函数......

回答by Jean-Bernard Jansen

what you are trying to do is a synchronouscall, so its not a good idea to do it with a function designed for asynchronouscall.

您要做的是同步调用,因此使用专为异步调用设计的函数不是一个好主意。

You have an answer here : How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

您可以在这里找到答案:如何让 jQuery 执行同步而不是异步的 Ajax 请求?