当前位置:网站首页>Promise. All capture error
Promise. All capture error
2022-06-11 06:57:00 【Xiaoman's code world】
Promise.all By default, as long as there is an error, it will directly return the error .promise.all Any one of them promise When an error occurs, it will execute reject, As a result, other normally returned data cannot be used
Promise.all([
Promise.resolve(1),
Promise.reject('err'),
Promise.resolve(2)
]).then(res => {
console.log(res)
}).catch(err => {
console.log('error', err)
})
// The result returned to : error err
If our array has some errors but does not affect the return of other results , For each item promise Capture .
Promise.all([
Promise.resolve(1),
Promise.reject('err'),
Promise.resolve(2)].map(item => {
return item.catch(err => {
console.log('item error', err)
})
})
).then(res => {
console.log(res)
}).catch(err => {
console.log('error', err)
})
// The result returned to :
// item error err
// [1, undefined, 2]
In this way, it can be realized that no matter what happens , It does not affect the return of the final result .
Promise.all Simplified practical principles
function promiseAll(promises){
return new Promise((resolve,reject) => {
if (!Array.isArray(promises)) {
return reject(new TypeError("argument must be anarray"))
}
let countNum=0;
let promiseNum=promises.length;
let resolvedvalue=new Array(promiseNum);
for (let i=0; i<promiseNum; i++) {
Promise.resolve(promises[i]).then(value =>{
countNum++;
resolvedvalue[i]=value;
if(countNum===promiseNum){
return resolve(resolvedvalue)
}
},reason => {
return reject(reason)
})
}
})
}
边栏推荐
- Sohu employees encounter wage subsidy fraud. What is the difference between black property and gray property and how to trace the source?
- Zabbix 监控主机是否在线
- Starting from scratch (I)
- 【迅为干货】龙芯2k1000开发板opencv 测试
- Array de duplication....
- 搜狐员工遭遇工资补助诈骗 黑产与灰产有何区别 又要如何溯源?
- Difference between byte and bit
- 022 basic introduction to redis database 0
- What are the differences and usages of break and continue?
- Dynamic import
猜你喜欢

538. convert binary search tree to cumulative tree

Flat design, blog website (VIII) code source code

Transformer Tracking

563. slope of binary tree

Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically

Leetcode hot topic 100 topic 6-10 solution

Scripy web crawler series tutorials (I) | construction of scripy crawler framework development environment
![JS implementation of graphic merging and sorting process [source code attached]](/img/c8/210ddab791eb2319519496f7c7d010.jpg)
JS implementation of graphic merging and sorting process [source code attached]

Saltstack deployment LNMP

【迅为干货】龙芯2k1000开发板opencv 测试
随机推荐
Summary and review
Illustrate the principle of one-way linked list and the method of JS to realize linked list [with source code]
Drawing with qpainter
核查医药代表备案信息是否正确
saltstack部署zabbix状态文件编写
Sohu employees encounter wage subsidy fraud. What is the difference between black property and gray property and how to trace the source?
UEFI finding PCI devices
538. convert binary search tree to cumulative tree
WPF 数据绑定(四)
Count the time-consuming duration of an operation (function)
About the principle and code implementation of Siou (review IOU, giou, Diou, CIO)
JS two methods to determine whether there are duplicate values in the array
Moment time plug-in tips -js (super detailed)
VTK vtkplane and vtkcutter use
WPF data binding (IV)
VTK-vtkPlane和vtkCutter使用
SQL language - query statement
saltstack的常用模块
The nearest common ancestor of 235 binary search tree
Won't virtual DOM be available in 2022? Introduction to virtual Dom and complete implementation of diff and patch