Promise all 特性(只要失败一个就不会走then)的解决方案

Promise all    打包几个接口一起请求。

但是其中如果有一个接口失败了那么就不会继续走下去了  会影响到其他接口操作,关联性太强了,

需求: 前面打包的的几个接口操作无论失败或者成功几个都需要走后面的接口,就会有点麻烦了,

解决:在catch里面resolve就行了

const  p1 =  new Promise(resolve => {
    const a =b;
    resolve(a);
}).catch(()=>{
    return Promise.resolve('aaab')
});
const  p2 =  new Promise(resolve => {
    const a =1;
    return resolve(a);
}).catch(()=>{
    return Promise.resolve('aaa')
});


Promise.all([p1,p2]).then((data)=>{
    console.log('then 成功',data);
}).catch((err)=>{
    console.log('333');
    console.log('errr',err);
})

 

打印效果

Promise all 特性(只要失败一个就不会走then)的解决方案_第1张图片

你可能感兴趣的:(JavaScript)