// 循环串执行
let a = []
const arr = [{
"name": "hosts.doc",
"url": "/storage/emulated/0/ccs/hosts.doc",
"sizeMB": "0.0009"
}, {
"name": "hosts.pdf",
"url": "/storage/emulated/0/ccs/hosts.pdf",
"sizeMB": "0.0009"
}]
function get(e){
// 网络请求
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(e)
}, 500)
})
}
async function run(){
let users = [{
"name": "hosts.doc",
"url": "/storage/emulated/0/ccs/hosts.doc",
"sizeMB": "0.0009"
}, {
"name": "hosts.pdf",
"url": "/storage/emulated/0/ccs/hosts.pdf",
"sizeMB": "0.0009"
}]
let nameArray = [];
for (item of users){
console.log(item)
const name = await get(item.name)
nameArray.push(name);
}
console.log(nameArray);
}
run()
// console.log(a)
// arr.forEach((n,i)=>{
// get(i).then(res=>{
// console.log(res)
// a.push(res)
// })
// })
// a获取正确的值需要在arr遍历结束后再执行
// 怎么知道上面的遍历结束,或者遍历换一种写法?
console