呼叫异步JavaScript功能同步方式 [英] Call An Asynchronous Javascript Function Synchronously

查看:86
本文介绍了呼叫异步JavaScript功能同步方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是做了错误的方式上,目的是改造一个异步调用到一个非常同步codeBase的是几千行代码和时间目前还无法承受能力的一个非常特殊的情况下,进行更改为做是正确的。它伤害了我生命的每一根纤维,但现实与理想往往不网格。我知道这很烂。

OK,说出来的样子,我怎么让这个我可以:

 函数DoSomething的(){  VAR的数据;  回调函数(D){
    数据= D;
  }  myAsynchronousCall(参数1,回调);  //此处块,当回调结束后返回数据
  返回的数据;
}

的实施例(或缺乏)的所有使用库和/或编译器,这两者都是不可行的该溶液中。我需要如何使它块(例如不离开DoSomething的函数,直到回调被调用),而冻结UI一个具体的例子。如果这样的事情是可能的JS。


解决方案

  

别告诉我我应该怎么只管去做正确的方式或任何


确定。 <子> ,但你真的应该做的正确的方式......或任何


  

我需要的是如何让它阻止...不结冰的UI一个具体的例子,如果这样的事情是在JS可能的。


没有,这是不可能阻止的运行的JavaScript而不阻塞UI

由于缺乏信息,这很难提供一个解决方案,但一个选项可能是有调用函数做一些查询来检查一个全局变量,然后有回调集数据到全球。

 函数DoSomething的(){      //回调所接收的数据设置为一个全局变量
  回调函数(D){
      window.data = D;
  }
      //启动异步
  myAsynchronousCall(参数1,回调);}  //启动功能
做一点事();  //确保全球清楚
window.data = NULL  //直到数据在全球发现以一定间隔启动轮询
VAR INTVL =的setInterval(函数(){
    如果(window.data){
        clearInterval(INTVL);
        的console.log(数据);
    }
},100);

所有这些假设当然,你可以修改 DoSomething的()。我不知道这是在打牌。

如果它可以被修改,那么我不知道你为什么会不只是传递一个回调 DoSomething的()来从其它回调调用,但之前我惹上麻烦我最好停止。 ;)


呵呵,到底是什么。你给这表明它可以被正确完成一个例子,所以我要表明的解决方案...

 函数DoSomething的(FUNC){  回调函数(D){
    FUNC(D);
  }  myAsynchronousCall(参数1,回调);}DoSomething的(功能(数据){
    的console.log(数据);
});

由于您的示例包括传递给异步调用的回调,正确的方法是将传递给函数DoSomething的()来从回调调用。

当然,如果是这样的回调是做的唯一的事情,你只是通过 FUNC 直接...

  myAsynchronousCall(参数1,FUNC);

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;
}

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.

解决方案

"don't tell me about how I should just do it "the right way" or whatever"

OK. but you should really do it the right way... or whatever

" I need a concrete example of how to make it block ... WITHOUT freezing the UI. If such a thing is possible in JS."

No, it isn't possible to block the running JavaScript without blocking the UI.

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);

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

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);
});

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.

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天全站免登陆