同步调用异步 Javascript 函数 [英] Call An Asynchronous Javascript Function Synchronously

查看:42
本文介绍了同步调用异步 Javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是一个非常特殊的案例,故意以错误的方式将异步调用改造成一个非常同步的代码库,该代码库长达数千行,时间目前无法进行更改做对了".它伤害了我的每一根纤维,但现实和理想往往无法融合.我知道这很糟糕.

First, this is a very specific case of doing it the wrong way on-purpose to retrofit an asynchronous call into a very synchronous codebase that is many thousands of lines long and time doesn't currently afford the ability to make the changes to "do it right." It hurts every fiber of my being, but reality and ideals often do not mesh. I know this sucks.

好吧,顺便说一句,我该如何做到这一点:

OK, that out of the way, how do I make it so that I could:

function doSomething() {

  var data;

  function callBack(d) {
    data = d;
  }

  myAsynchronousCall(param1, callBack);

  // block here and return data when the callback is finished
  return data;
}

示例(或缺少示例)都使用库和/或编译器,这两者都不适用于此解决方案.我需要一个具体的例子来说明如何在不冻结 UI 的情况下阻止它(例如,在调用回调之前不要离开 doSomething 函数).如果这样的事情在 JS 中是可能的.

The examples (or lack thereof) all use libraries and/or compilers, both of which are not viable for this solution. I need a concrete example of how to make it block (e.g. NOT leave the doSomething function until the callback is called) WITHOUT freezing the UI. If such a thing is possible in JS.

推荐答案

不要告诉我我应该如何以正确的方式"或其他方式去做"

好的.但你真的应该以正确的方式去做......或者其他什么

我需要一个具体的例子来说明如何阻止它......而不冻结 UI.如果在 JS 中这样的事情是可能的."

不,不可能在不阻止 UI 的情况下阻止正在运行的 JavaScript.

No, it is impossible to block the running JavaScript without blocking the UI.

鉴于缺乏信息,很难提供解决方案,但一种选择可能是让调用函数进行一些轮询以检查全局变量,然后让回调将 data 设置为全球.

Given the lack of information, it's tough to offer a solution, but one option may be to have the calling function do some polling to check a global variable, then have the callback set data to the global.

function doSomething() {

      // callback sets the received data to a global var
  function callBack(d) {
      window.data = d;
  }
      // start the async
  myAsynchronousCall(param1, callBack);

}

  // start the function
doSomething();

  // make sure the global is clear
window.data = null

  // start polling at an interval until the data is found at the global
var intvl = setInterval(function() {
    if (window.data) { 
        clearInterval(intvl);
        console.log(data);
    }
}, 100);

所有这些都假设您可以修改doSomething().我不知道这是否在卡片中.

All of this assumes that you can modify doSomething(). I don't know if that's in the cards.

如果可以修改,那么我不知道你为什么不只是将回调传递给 doSomething() 以从另一个回调中调用,但我最好在我得到之前停下来陷入麻烦.;)

If it can be modified, then I don't know why you wouldn't just pass a callback to doSomething() to be called from the other callback, but I better stop before I get into trouble. ;)

哦,这到底是怎么回事.您给出的示例表明它可以正确完成,因此我将展示该解决方案...

Oh, what the heck. You gave an example that suggests it can be done correctly, so I'm going to show that solution...

function doSomething( func ) {

  function callBack(d) {
    func( d );
  }

  myAsynchronousCall(param1, callBack);

}

doSomething(function(data) {
    console.log(data);
});

因为您的示例包含传递给异步调用的回调,正确的方法是将函数传递给 doSomething() 以从回调中调用.

Because your example includes a callback that is passed to the async call, the right way would be to pass a function to doSomething() to be invoked from the callback.

当然,如果这是回调唯一要做的事情,您只需直接传递 func...

Of course if that's the only thing the callback is doing, you'd just pass func directly...

myAsynchronousCall(param1, func);

这篇关于同步调用异步 Javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆