【发布时间】:2017-12-15 19:51:40
【问题描述】:
我正在创建一个 API 服务器,在其中,我需要调用在另一个 API 服务器上找到的转换方法来获取负值。我相信我的代码是正确的,但是由于 ASYNC,我相信它会在进行所有转换之前返回值。代码如下:
for(let i = 0; i < results.length; i++) {
if (parseInt(results[i]['ACCOUNT_ID']) < 0) {
let account = axios.get('http://localhost:54545/api?request=convert&id='+results[i]['ACCOUNT_ID'])
.then(function (response) {
results[i]['ACCOUNT_ID'] = response.data.stringId;console.log(response.data.stringId);
})
.catch(function (error) {
console.log(error);
});
}
}
res.setHeader('Content-Type', 'application/json');
return res.status(200).json(results);
我想我需要以某种方式使用 Promise.all,但我不确定如何使用它。
任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: javascript node.js promise axios