【发布时间】:2017-05-15 02:40:31
【问题描述】:
我尝试使用异步 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);
}
})
有任何想法或其他方法可以得到相同的结果吗?
非常感谢
【问题讨论】:
-
您的代码有什么问题或您要解决的问题吗?
-
我正在尝试在一系列 console.log 记录我需要的所有内容之后呈现索引,但目前索引在日志结束之前呈现自身
标签: javascript node.js asynchronous