promise.all中一些不重要的接口报错不影响promise;但是又不想因为自己写的代码影响其他逻辑,可以写一个hack函数处理一下
const f1 = function () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('yinyin-1');
}, 1000);
});
};
const f2 = function () {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.9) {
resolve('yinyin-2');
} else {
reject('yinyin-error');
}
}, 1000);
});
};
//增加一个hack函数
const hack = function (promise) {
return new Promise((resolve) => {
promise
.then((data) => {
resolve({
yinyin: true,
value: data,
});
})
.catch(() => {
resolve({
yinyin: false,
value: '我的错误返回,看到我做证明不重要的函数异常了',
});
});
});
};
// 注意此处
Promise.all([f1(), hack(f2())])
.then((res) => {
console.log('success', res);
})
.catch((err) => {
console.log('error', err);
});