利用Promise实现Promise.all


let pall = function(...args){ let Res = [] let Err = [] return new Promise((resolve,reject)=>{ for(let i=0;i<args.length;i++){ args[i].then(res=>{ Res.push(res) if(Res.length == args.length){ resolve(Res) } }).catch(err=>{ Err.push(err) reject(Err) }) } }) } let p1 = new Promise((resolve,reject)=>{ setTimeout(()=>{ resolve("p1") },1000) }) let p2 = new Promise((resolve,reject)=>{ setTimeout(()=>{ resolve("p2") },500) }) pall(p1,p2).then(res=>{ console.log(res) }).catch(err=>console.log(err))

 

posted @ 2021-03-02 16:54  JS-Feng  阅读(91)  评论(0编辑  收藏  举报