Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX异步回调工作正常,但如何等待返回值_Javascript_Ajax_Extjs - Fatal编程技术网

Javascript AJAX异步回调工作正常,但如何等待返回值

Javascript AJAX异步回调工作正常,但如何等待返回值,javascript,ajax,extjs,Javascript,Ajax,Extjs,我的主要EXTJS函数是调用三个不同的AJAX异步函数。好消息是,每个AJAX函数都成功返回一个关于AJAX GET调用是否成功的布尔回调 问题是,一旦从所有三个AJAX函数返回布尔结果,我需要从主调用函数执行额外的post业务逻辑 但由于它是异步的,所以代码不会等待异步函数完成,而是会快速通过 关于如何让代码优雅地等待状态类型不再是“未定义”的问题,有什么想法吗 我现在不在我的代码附近,但这里是一个粗略的近似值 function main(){ var status1, status2,

我的主要EXTJS函数是调用三个不同的AJAX异步函数。好消息是,每个AJAX函数都成功返回一个关于AJAX GET调用是否成功的布尔回调

问题是,一旦从所有三个AJAX函数返回布尔结果,我需要从主调用函数执行额外的post业务逻辑

但由于它是异步的,所以代码不会等待异步函数完成,而是会快速通过

关于如何让代码优雅地等待状态类型不再是“未定义”的问题,有什么想法吗

我现在不在我的代码附近,但这里是一个粗略的近似值

function main(){
  var status1, status2, status3, result1, result2,result3;

    thisIsAsynchFunction1(args,function(result1){
        status1 = result1;
    });
    thisIsAsynchFunction2(args,function(result1){
        status2 = result1;
    });

    thisIsAsynchFunction1(args,function(result1){
        status3 = result1;
    });

    // Perform logic based on callback from functions above

}

谢谢。

您不能让Javascript等待。这根本不起作用。当您发现所有三个异步结果都完成时,您可以做的是触发您的最终操作。如果不重新设计代码以使用承诺(这将是实现这一点的现代方式),您可以执行以下操作:

function main(callbackFn){
  var status1, status2, status3, doneCnt = 0;

    thisIsAsynchFunction1(args,function(result1){
        status1 = result1;
        ++doneCnt;
        checkDone();
    });
    thisIsAsynchFunction2(args,function(result1){
        status2 = result1;
        ++doneCnt;
        checkDone();
    });

    thisIsAsynchFunction1(args,function(result1){
        status3 = result1;
        ++doneCnt;
        checkDone();
    });

    // Perform logic based on callback from functions above
    function checkDone() {
        // if all three results are done, then call the callback
        if (doneCnt === 3) {
            callbackFn(status1, status2, status3);
        }
    }
}

// call main and pass it a callback function
main(function(s1, s2, s3) {
    // all three async results are available here
});

// Be careful because code here will execute BEFORE the async results are done
// any code that must wait for the async results must be in the above callback

一种较新的方法是让每个异步操作返回一个承诺,当每个异步操作完成时,它将用其结果解析其承诺。然后,您可以使用承诺库函数(通常称为
.when()
)在所有三个承诺都完成时触发回调。许多现代JS库已经从ajax调用(如jQuery)返回承诺,因此承诺已经存在

我自己也不知道ExtJS,但查看了它的文档,还没有看到promise支持,我发现有一个线程在讨论promises不会出现在版本5中这一事实。因此,也许您只需要使用上面的计数器设计,或者ExtJS在多个异步操作全部完成时有一些自己的监控支持。