给出
const list = [1,2,3,4,5,6,7];
let results = [];
和一个功能
powerNumPlusOne = async(num) : Promise<any> => {
return powerNum*powerNum + 1;
}
如何确保此代码有效
list.forEach(async function(i){
results.push( await this.powerNumPlusOne(i));
})
和results
应该按顺序[2,5,10,17,26,37,50]
吗?
答案 0 :(得分:-1)
您可以使用Promise.all()。它接受许多动作,并在全部完成后以完全相同的顺序返回它们。
const list = [1,2,3,4,5,6,7];
let actions = list.map(powerNumPlusOne);
let results = await Promise.all(actions);
enter code here