当前位置:网站首页>Handwritten promise all
Handwritten promise all
2022-06-25 05:05:00 【I am Feng Feng Yi】
/** * * @param {iterator} proms */
Promise.all = function (proms) {
return new Promise((resolve, reject) => {
try {
let count = 0; // How many proms
let resolvedCount = 0; // How many promise already resolve
let results = [] // resolve The result is an array
for (const pro of proms) {
// proms Not necessarily arrays , Should be an iterator
let curIndex = count; // Record this promise resolve Then return the index of the array
count++;
Promise.resolve(pro).then(data => {
// It may not be one promise, So we need to pack it
results[curIndex] = data;
resolvedCount++;
if (count === resolvedCount) {
// all promise already resolved, Trigger return Promise Of resolve
resolve(results);
}
}, reject) // If there is an error, call reject
}
if (count === 0) {
// It is possible that an empty array is passed in
resolve(results);
}
} catch (error) {
reject(error) // If an error occurs in the process , Direct trigger return promise Of reject
}
})
}
Promise.all([1,2,Promise.resolve(3),Promise.reject(0)]).then(data=>{
console.log(data);
}).catch(err=>{
console.log(err);
})
Promise.all(null).then(data=>{
console.log(data);
}).catch(err=>{
console.log(err);
})
边栏推荐
- Compatible with Internet Explorer
- PHP calls map API
- Abuse unlimited authorization -- is your address safe?
- TX Text Control 30.0 ActiveX
- Customize the console plot result style
- 魔法猪系统重装大师怎么使用
- 【FLink】access closed classloader classloader. check-leaked-classloader
- Heavy broadcast | phase shift method + mathematical principle derivation of multi frequency heterodyne + implementation
- epplus复制模板后打印区域变小的问题
- Database query optimization method
猜你喜欢

Working principle of asemi three-phase rectifier bridge

A review of small sample learning

基于SSH实现的学生成绩管理系统

Kotlin compose perfect todo project surface rendering background and shadow

Startup mode of SoC verification environment

Two hours to take you into the software testing industry (with a full set of software testing learning routes)

Read the general components of antd source code

Wechat applet new version prompt update

固态硬盘开盘数据恢复的方法

Leader: who can use redis expired monitoring to close orders and get out of here!
随机推荐
IronOCR 2022.1 Crack
Integrate CDN to create the ultimate service experience for customers!
PHP uses JWT
固态硬盘开盘数据恢复的方法
Web3 DAPP user experience best practices
ASEMI大功率场效应管和三极管的区别
Google Earth engine (GEE) - Global jrc/gsw1_ 1 / batch download of yearlyhistory dataset (China region)
buuctf(re)
CTFHub-rce
Prototypical Networks for Few-shot Learning
Flex flexible layout for mobile terminal page production
Bind simulation, key points of interpreting bind handwritten code [details]
JDBC (IV)
Difference between asemi high power FET and triode
OOP stack class template (template +ds)
Drag modal box
Virtual honeypot Honeyd installation and deployment
Mysql interactive_ Timeout and wait_ Timeout differences
epplus复制模板后打印区域变小的问题
February 19 CTF exercise