3 回答
TA贡献1836条经验 获得超13个赞
function app (num, data) {
return new Promise(function (resolve, reject) {
resolve(num)
})
}
let data = [1, 2, 3]
let res = Promise.resolve()
for (let index = 0; index < data.length; index++) {
res = res.then(v => {
console.log(v)
return app(index)
})
}
TA贡献1921条经验 获得超9个赞
如果你每次执行app()互相没有联系 可以放在一个数组里 用Promise.all来执行
let p =[]
for(let i=0;i<data.length;i++){
p.push(app(i,data))
}
Promise.all(p).then(res=>{
console.log(res)
})
TA贡献1835条经验 获得超7个赞
function app(num, data) {
return new Promise(function (resolve, reject) {
resolve(num)
})
}
function test(arr, cb) {
return arr.reduce((p, v) => p.then(() => cb(v)), Promise.resolve())
}
test([4, 1, 9], function (num) {
return app(num).then(res => {
console.log(res)
});
})
添加回答
举报