问题描述
我也想寻求帮助和解释,为什么会发生以下情况。
var list = []
if (undefined !== resp.data) {
Array.from(resp.data).forEach(async (data) => {
let id = data.id
await axios.get(`http://<server>:<port>/api/${id}`,{
headers: {
'Authorization': `${req.headers.authorization}`
}
}).then(resp => {
list.push(id)
}).catch(err => {
console.log(`error ${err}`)
})
console.log(JSON.stringify(list))
})
}
console.log('Last : ' + JSON.stringify(list))
在上述情况下,假设axios获取请求成功,因此它将在list
中添加ID。
,但是最后的输出仍然为空[]
。
我也观察到一些奇怪的情况
您可以看到,即使我在此处添加了console.log('Last : ' + JSON.stringify(list))
关键字,也首先执行了await
。
最后,是否可以解决此问题?我只想在列表末尾添加项目。 Array.from.forEach()
之前的
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)