hello~同学们好,今天分享一个js的Promise.all(), 当其中一个为失败状态如何正常获取其他值的小技巧.
这是原来的代码段,可以看到 使用Promise.all 发送了3个携带id的请求
// 使用Promise.all 其中id为69的商品,返回失败,会导致整个Promise接受到reject状态.
// 所以进行改造, p catch 得到的err 为返回失败抛出的信息, 进行置空
`
Promise.all([
getProductDetail(this.wybWuyiId, from),
getProductDetail('88', from),
getProductDetail('69', from)
].map(p => p.catch(err => '')))
.then(res => {
res.map(item => {
this.wuyiActivityList.push(item.data.storeInfo)
})
})
.catch(err => {
// console.log('err',err);
})
`