【发布时间】:2017-05-14 04:19:33
【问题描述】:
所以我是 Promise 的新手,我想使用 Promise 进行多个 url 调用。现在看来算法可行,但问题是 riot api 有速率限制,所以我必须让程序在每次 api 调用后等待一小段时间。我应该如何添加这个?
var matchidList = [];
for (var i = 0; i < pml.length; i++) {
matchidList.push(pml[i]["gameId"]);
}
var matchuri = "https://na1.api.riotgames.com/lol/match/v3/matches/";
var uris = [];
for (var i = 0; i < matchidList.length; i++) {
uris.push(matchuri + matchidList[i] + "?" + apikey);
}
Promise.map(uris, function(url) {
return rp.getAsync(url).spread(function(response,body) {
return JSON.parse(body);
});
}).then(function(results) {
// results is an array of all the parsed bodies in order
console.log(results);
tag.checkTags("").then((tags) =>{
res.render('index', { result: body,
val: search,
matches: ''
});
});
}).catch(function(err) {
console.log(err);
// handle error here
});
【问题讨论】: