如何知道什么时候多个异步调用完成,然后调用另一个命令? [英] How to know when multiple asynchronous calls to complete then call another command?

查看:124
本文介绍了如何知道什么时候多个异步调用完成,然后调用另一个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使得JavaScript和Ajax多个异步调用的时候越来越麻烦。一切都像在同一时间开始在多个线程和输出依赖于它到达的时间。

I am getting trouble when making multiple asynchronous call in javascript and ajax. Everything look like start in multiple thread at the same time and output depend on the time it reach.

电流输出是:

DONE
UpdateUserLocation...
UpdateUserFriends...
UpdateUserFriendList...
UpdateUserAlbum...
UpdateUserGroup...

但预期是

UpdateUserLocation...
UpdateUserFriends...
UpdateUserFriendList...
UpdateUserAlbum...
UpdateUserGroup...
DONE

我的code:

{...
    HandleExtendAccessToken(accessToken, fbUserId, fbName, fbEmail, function(result){

        if(result == true) {
            console.log("DONE");   
        }
    });
...};

function HandleExtendAccessToken(accessToken, fbUserId, fbName, fbEmail, callback){

    FBExtendAccessToken.ExtendCurrentToken60Days(accessToken, fbUserId, fbName, fbEmail);


    if(dayUpdateUserLocation == '') UpdateUserLocation();

    if(dayUpdateUserFriends == '') UpdateUserFriends();

    if(dayUpdateUserFriendList == '') UpdateUserFriendList();

    if(dayUpdateUserAlbum == '') UpdateUserAlbum();

    if(dayUpdateUserGroup == '') UpdateUserGroup();

    if(dayUpdatePage == '') UpdatePage();

    callback(true);
};

难道我错了什么东西?我该如何解决它。谢谢

Did I wrong something? How do I fix it. Thanks

更新:

这是上面的列表中的一个例子更新功能。一个功能我做2 AJAX调用:Facebook的API AJAX来获取信息,另一种是AJAX来更新这些信息到我的服务器。完成后获取的Facebook信息和更新服务器,我想显示DONE或重定向用户到另一页:

This is an example update function on the list above. One function I make 2 ajax call: Facebook API AJAX to get information, another is AJAX to update those info to my server. When complete retrieve facebook information and update to server, I want to display DONE or redirect user to another page:

function UpdateUserLocation(){

    var userLocationIds = "";
    var userLocationNames = "";
    FB.api('/me/locations' + '?access_token=' + accessToken, function(response) {
        var locationList = response.data;
        var userLocationList = "";

        if(locationList !=undefined && locationList.length != 0){
            for (var index = 0; locationList.length > index; index++){
                if(index == 0){
                    userLocationIds = locationList[index].place.id;
                    userLocationNames= locationList[index].place.name;
                } else {
                    userLocationIds = userLocationIds + "-|-" +      locationList[index].place.id;
                    userLocationNames = userLocationNames + "-|-" +  locationList[index].place.name;
                }
            }
        } else {
            userLocationIds = "108153009209321";
            userLocationNames = "Viet Nam";
        }
        //Save user location to server
        FBData.UpdateFBData(fbUserId, userLocationIds,userLocationNames, 1);
        console.log("UpdateUserLocation...");

    });  
}

您可以在这里尝试应用: http://www.webdoanhnghiep.biz/Facebook-Amplifier的.aspx 并感谢你的时间帮助我。

You can try the app here: http://www.webdoanhnghiep.biz/Facebook-Amplifier.aspx and thank for your time on help me out.

推荐答案

jQuery的引入递延对象,它可以让你执行一系列的方法,随后通过某种做过回调。

jQuery introduces the deferred object which allows you to execute a series of methods followed by some sort of done callback.

http://api.jquery.com/category/deferred-object/

http://api.jquery.com/deferred.done/

http://api.jquery.com/deferred.fail/

async.js 是用于执行一系列异步命令后跟一个回调的另一个库。

async.js is another library for executing a series of asynchronous commands followed by a single callback.

async.parallel([
    function(){ ... },
    function(){ ... }
], callback);

async.series([
    function(){ ... },
    function(){ ... }
]);

这篇关于如何知道什么时候多个异步调用完成,然后调用另一个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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