SOURCE

// p1:立即成功
const p1 = Promise.resolve("p1_success");
// p2:2 秒后成功
const p2 = new Promise((resolve, reject) => setTimeout(resolve, 2000, 'p2_success'));
// p3:1 秒后失败
const p3 = new Promise((resolve, reject) => setTimeout(reject, 1000, 'p3_success'));

function allSettled(promises){
    const result = new Array(promises.length);
    let count = 0;
    return new Promise((resolve,reject)=>{
        for(let i =0;i<promises.length;i++){
            let p =promises[i];
            if(p&&typeof p.then === 'function'){
                p.then((data)=>{
                    result[i] = {status:'fufilled',value:data};
                    count++;
                    if(count === promises.length){
                        resolve(result)
                    }
                }).catch(err=>{
                    result[i] = {status:'rejected',reason:err};
                    count++;
                    if(count === promises.length){
                        resolve(result)
                    }
                })
            }else{
                result[i] = {status:'fulfilled',value:p};
                count++;
                if(count === promises.length){
                    resolve(result)
                }
            }
        }
    })
}
console.log(allSettled([p1, p2, p3, 1]).
  then((results) => results.forEach((result) => console.log(result))));
console 命令行工具 X clear

                    
>
console