我以为这会在Promise.all
catch块中引发错误,但是它永远不会那么远吗?
我试图了解如何处理通话中以及Promise.all
上被拒绝的诺言。
const apiCallOne = new Promise((resolve, reject) => (
resolve('Resolved !!!')
)).then(console.log)
.catch(console.warn);
const apiCallTwo = new Promise((resolve, reject) => (
reject('Rejected !!!')
)).then(console.log)
.catch(console.warn);
Promise.all([apiCallOne, apiCallTwo])
.then(value => console.log('all', value))
.catch(err => console.error('error', err));
Promise.all
会碰到它的捕获块吗?
答案 0 :(得分:0)
不,catch
不会吞没错误,就像您所说的Promise.all
catch块将永远不会被调用一样,以下内容将根据您的期望工作
const apiCallOne = new Promise((resolve, reject) => (
resolve('Resolved !!!')
)).then(console.log)
.catch((err) => {
console.warn(err)
throw err
});
const apiCallTwo = new Promise((resolve, reject) => (
reject('Rejected !!!')
)).then(console.log)
.catch((err) => {
console.warn(err)
throw err
});
Promise.all([apiCallOne, apiCallTwo])
.then(value => console.log('all', value))
.catch(err => console.error('error', err));
答案 1 :(得分:0)
您可以在调用Promise.all
时处理它们,无需在执行请求之前处理它们,因为我们希望并行运行它们,并在出现错误时将其全部取消。
const apiCallOne = new Promise((resolve, reject) => (
resolve('Resolved !!!')
))
const apiCallTwo = new Promise((resolve, reject) => (
reject('Rejected !!!')
))
Promise.all([apiCallOne, apiCallTwo])
.then(value => console.log('all', value))
.catch(err => console.error('error', err));