简体   繁体   English

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

[英]AJAX asynch callback working correctly but how do I wait for the returned values

My main EXTJS function is calling three different AJAX asynch functions.The good news is that each AJAX function is successfully returning a boolean callback on whether the AJAX GET call was successful or not. 我主要的EXTJS函数正在调用三个不同的AJAX异步函数,好消息是每个AJAX函数都成功返回了有关AJAX GET调用是否成功的布尔回调。

The problem - I need to perform additional post business logic from my main calling function once the boolean results have been returned from all three AJAX functions. 问题-一旦所有三个AJAX函数都返回了布尔结果,我就需要从我的主调用函数中执行附加的后期处理逻辑。

But as it is async the code will not wait for the async functions to complete and will fly through. 但是由于它是异步的,所以代码将不会等待异步功能完成而飞走。

Any ideas on how I can get my code to GRACEFULLY wait until the typeof status is no longer 'undefined' ? 关于如何将代码添加到GRACEFULLY的任何想法都等到typeof状态不再为'undefined'为止?

I am not near my code right now but here is a rough approximation. 我现在不在我的代码旁,但这是一个大概的近似值。

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

}

Thank You. 谢谢。

You cannot make Javascript wait. 您不能等待Javascript。 It simply doesn't work that way. 这样根本行不通。 What you can do is to trigger your final action when you discover that all three async results are done. 当发现所有三个异步结果都已完成时,您可以做的是触发最终操作。 Without redesigning your code to use promises (which would be the modern way to do this), you could do something like this: 无需重新设计代码以使用Promise(这是实现此目标的现代方法),您可以执行以下操作:

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

A newer type of approach would be to have each async operation return a promise and when each async operation completes it would resolve its promise with its result. 一种较新的方法是让每个异步操作返回一个promise,当每个异步操作完成时,它将用其结果来解决其promise。 Then, you could use a promise library function (often called .when() to trigger a callback when all three promises were done. Many modern day JS libraries already return promises from ajax calls (such as jQuery) so the promise is already there. 然后,您可以使用Promise库函数(通常在调用所有三个.when()调用.when()来触发回调。许多现代JS库已经从Ajax调用(例如jQuery)返回Promise,因此Promise已经存在。

I don't know ExtJS myself, but looked in its documentation and didn't see promise support yet and I found one thread discussing the fact that promises were not going to be in version 5. So, perhaps you just go with the counter design above or maybe ExtJS has some of its own support for monitoring when multiple async operations are all done. 我本人不了解ExtJS,但查看了它的文档,还没有看到Promise支持,我找到了一个线程来讨论Promise不会在版本5中的事实。因此,也许您可​​以使用计数器设计以上,或者ExtJS拥有自己的一些支持,可以监视何时完成多个异步操作。

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

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