【发布时间】:2019-10-14 22:17:44
【问题描述】:
我有以下代码:
* Fetch stats from api
*/
fetchStats() {
this._isFetching = true;
// fetch stats after building url and replacing invalid characters
return new Promise(async (resolve, reject) => {
await API.fetchStats(this.rsn)
.then(jres => {
this.skills = jres.main.skills;
this._isFetching = false;
resolve('success');
})
.catch(err => {
console.log(err);
console.log('error retreiving stats');
this._isFetching = false;
reject('Failed to retreive stats');
})
.finally(() => {
this._isFetching = false;
});
});
}
我认为让它与 await 异步会使它等到它得到响应后再继续。返回承诺是我在测试中添加的内容,以查看是否可以使其同步。
然后我的代码使用了这个方法:
memberCollection.forEach(async el => {
await el.player.fetchStats()
.then(() => {
console.log(`Refreshed ${el.player.rsn}'s account`);
})
.catch(console.log(`Failed to refresh ${el.player.rsn}'s account`));
});
我的想法是,它会等到它得到响应,然后console.log 要么刷新成功,要么刷新失败。相反,我看到的是一大堆“成功”消息,然后是一串失败的消息,表明它在 foreach 中同时运行 then 和 catch 消息。有谁知道我怎样才能完成这项工作。
如果我导航到API URL 手动它工作得很好,如果我只做一个成员(而不是 forEach)它工作得很好。所以我试图限制一次发出的请求数量。我尝试将我的 axios 超时设置为 10、20 和 60 秒,但没有任何改善。
解决方案代码:
const asyncForEach = async (arr, cb) => {
for(let i=0;i<arr.length;i++) {
let el = arr[i];
try {
let res = await cb(el);
} catch (err) { console.log(err) };
if(el.player && el.player.rsn) console.log(`Processed ${el.player.rsn}`);
}
console.log('done processing in asyncForEach');
}
【问题讨论】:
标签: javascript node.js axios