How to know when multiple asynchronous calls to co

2019-08-19 02:53发布

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.

Current the output is:

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

But expected are

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

My 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

UPDATE:

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

    });  
}

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

1条回答
我只想做你的唯一
2楼-- · 2019-08-19 03:02

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 is another library for executing a series of asynchronous commands followed by a single callback.

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

async.series([
    function(){ ... },
    function(){ ... }
]);
查看更多
登录 后发表回答