<script>
let arr = [{name:1,chi:[{name:7}]},{name:4,chi:[{name:4}]},{name:5,chi:[{name:5}]},{name:6,chi:[{name:4}]}]
async function text() {
await Promise.allSettled(arr.map(async item1=>{
//第一层返回
await Promise.allSettled(
item1.chi.map(async item => {
//第二层
item.key = await fetch(item.name);
}))
}))
console.log(arr); //三秒后打印出数值
}
function fetch(x) {
return new Promise((resolve, reject) => {
setTimeout(()=>{
resolve(x)
},3000)
})
}
text();
</script>
使用pormise.all async/await 进行多层级循环多次请求
最新推荐文章于 2022-09-23 21:54:07 发布