简体   繁体   中英

Async callback nodejs

I try to use async NodeJs library to execute a render with express after collecting informations of a API but the async.parallel callback execute itself before collecting all data that I need

This is the code :

            LolApi.getMatchHistory(app_dtb.summoner.id, "euw", function(err, history) {
            if (!err) {
                async.parallel([
                    function(callback) {
                        LolApi.getMatch(history.matches[nbMatch].matchId, function(err, match) {
                            if (!err) {
                                var teamIn;

                                function getParticipantNb() {
                                    for (var i = 0; i < match.participantIdentities.length; i++) {
                                        if (app_dtb.summoner.id == match.participantIdentities[i].player.summonerId) {
                                            if (i <= 5) teamIn = 100
                                            else teamIn = 200
                                            return i + 1
                                        }
                                    }
                                    return false;
                                }

                                var participantNb = getParticipantNb()
                                if (match.teams[0].winner == true && teamIn == 100) {
                                    app_dtb.lastGame.won = "Win";
                                } else {
                                    app_dtb.lastGame.won = "Loose";
                                }
                                console.log(app_dtb.lastGame.won)
                            } else {
                                console.log(err)
                            }

                        });
                        setTimeout(function() {
                            callback(null, "one");
                        }, 200);
                    },
                    function(callback) {
                        options = {
                            champData: 'allytips,blurb',
                            version: '4.4.3',
                            locale: 'en_US'
                        }
                        LolApi.Static.getChampionById(history.matches[nbMatch].champion, options, function(err, champ) {
                            if (!err) {
                                console.log(champ.name);
                                app_dtb.lastGame.champName = champ.name

                            } else {
                                console.log(err)
                            }

                        });
                        setTimeout(function() {
                            callback(null, "two");
                        }, 100);
                    }
                ], function(err, results) {
                    console.log(results)
                    res.render("index");
                });
            } else {
                console.log(err);
            }

        })

Any idea or other way to have the same result ?

Thanks a lot

You should call the callback inside your LolApi methods callback and be sour that the async callback will be called for two parallel functions eventually. so may the timeout called before your LolApi callbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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