当前位置:网站首页>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)
})
}
})
}
边栏推荐
- 开源漫画服务器Mango
- Do you use typescript or anyscript
- Cv2.rectangle() picture frame
- Leetcode hot topic 100 topic 6-10 solution
- Shuttle container component
- 常用问题排查工具和分析神器,值得收藏
- Mediaextractor source code analysis of multimedia framework analysis (1)
- 021 mongodb database from getting started to giving up
- Henan college entrance examination vs Tianjin college entrance examination (2008-2021)
- Array information management system reconfiguration preheating (1) how to write basic logic using linear continuous structure?
猜你喜欢

【概率论与数理统计】猴博士 笔记 p41-44 统计量相关小题、三大分布的判定、性质、总体服从正态分布的统计量小题

socket. IO cross domain stepping pit

争议很大的问题

Do you use typescript or anyscript

Open source cartoon server mango

双周投融报:资本抢滩元宇宙游戏

572. subtree of another tree

Scripy web crawler series tutorials (I) | construction of scripy crawler framework development environment

Sohu employees encounter wage subsidy fraud. What is the difference between black property and gray property and how to trace the source?

Leetcode hot topic 100 topic 21-25 solution
随机推荐
Throttling and anti shake
Drawing with qpainter
Detailed explanation of mutual call between C language and Lua
Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically
懒加载,预加载
[MATLAB image encryption and decryption] chaotic sequence image encryption and decryption (including correlation test) [including GUI source code 1862]
Summary of string processing skills II
arguments......
Henan college entrance examination vs Tianjin college entrance examination (2008-2021)
六月集训(第11天) —— 矩阵
SQL language - query statement
[MATLAB image fusion] particle swarm optimization adaptive multispectral image fusion [including source code phase 004]
Summary and review
核查医药代表备案信息是否正确
JS implementation of graphic merging and sorting process [source code attached]
Saltstack deployment LNMP
[Xunwei dry goods] opencv test of Godson 2k1000 development board
WPF data binding (IV)
Leetcode hot topic 100 topic 11-15 solution
This comprehensive understanding