Promise.allSettled()
Promise是对异步状态进行管理的
当这三个请求执行完就会进入.then,失败就会进入.catch 如果当前三个请求,其中有一个请求是失败的,就会认为整个请求都是失败的,这个情况使用Promise.all 就处理不了
Promise.all([
Promise.resolve({
code: 200,
data: [1, 2, 3]
})
Promise.reject({
code: 500,
data: []
})
Promise.resolve({
code: 200,
data: [7, 8, 9]
})
]).then(res =>{
console.log(res)
console.log('成功')
}).catch(err=>{
console.log(err)
console.log('失败')
})
allSettled
Promise.allSettled([
Promise.resolve({
code: 200,
data: [1, 2, 3]
})
Promise.reject({
code: 500,
data: []
})
Promise.resolve({
code: 200,
data: [7, 8, 9]
})
]).then(res =>{
console.log(res)
console.log('成功')
const data = res.filter(item => item.status == 'fulfilled')
console.log(data)
}).catch(err=>{
console.log(err)
console.log('失败')
})
使用allSettled不管成功还是失败里面都会返回status状态,可以使用filter过滤成功的状态