赞
踩
- Promise.all默认只要有一个错误就直接返回错误。promise.all中任何一个promise 出现错误的时候都会执行reject,导致其它正常返回的数据也无法使用
- Promise.all(
- [
- Promise.reject({ code: 500, msg: "服务异常" }),
- Promise.resolve({ code: 200, list: [] }),
- Promise.resolve({ code: 200, list: [] })
- ].map(p => p.catch(e => e))
- )
- .then(res => {
- console.log("res=>", res);
- })
- .catch(error => {
- console.log("error=>", error);
- });
- res=> [ { code: 500, msg: '服务异常' },
- { code: 200, list: [] },
- { code: 200, list: [] } ]
- 核心内容是map方法,map的每一项都是promise,catch方法返回值会被promise.reslove()包裹,这样传进promise.all的数据都是resolved状态的。
-
- // 使用Promise.all 其中id为69的商品,返回失败,会导致整个Promise接受到reject状态.
- // 所以进行改造, p catch 得到的err 为返回失败抛出的信息, 进行置空
- .map(p => p.catch(err => '')))

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。