// 模拟ajax
const ajax = (id,timeout=500) => () => new Promise(resolve=>setTimeout(resolve.bind(null,id), timeout));
// arr:参数数组
// fn:对每个参数需要执行的函数
const main = (arr,fn)=>{
const [values,reasons] = [[],[]];
return new Promise(resolve=>{
arr
.reduce((total,cur)=>(
total
.then(fn(cur))
.then(
// 如果不用bind,数组的push方法中的this会丢失
values.push.bind(values),
reasons.push.bind(reasons)
)
),Promise.resolve())
.then(resolve.bind(null,[values,reasons]))
})
}
main([1,2,4,4,5,6,7],ajax).then(console.log)