异步回调nodejs

时间:2016-12-30 20:26:12

标签: javascript node.js asynchronous

我尝试使用异步NodeJs库在收集API信息后使用express执行渲染,但async.parallel回调在收集我需要的所有数据之前自行执行

这是代码:

            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);
            }

        })

有任何想法或其他方式可以获得相同的结果吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

你应该在你的callback方法回调中调用LolApi,并且最终会为两个并行函数调用async回调。所以timeout可能会在LolApi回调之前调用。

相关问题