问题: axios已经取到值了,为什么打印的res 是空数组呢?
代码如下:
`
async function testDemo(){
let newNodes = [],
childNode = [{'name':'1',id:'1'},{'name':'11',id:'11'},{'name':'12',id:'12'}];
let childProm = childNode.forEach((child)=>{
return axios.post('/user', {
id: child.id
})
.then(function (result) {
newNodes = newNodes.concat(result.values);
})
})
await Promise.all([childProm])
return newNodes;
}
testDemo().then(res => {
console.log('res', res)
})
`
1、
Promise.all(e)
里的参数需为迭代对象2、
arr.forEach(callback(currentValue [, index [, array]])[, thisArg])
的值/返回值为undefined
。解决方案:使用Map方法