当前位置:网站首页>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);
})
边栏推荐
- 小白一键重装官网下载使用方法
- Kotlin compose perfect todo project surface rendering background and shadow
- Use js to simply implement the apply, call and bind methods
- ASEMI三相整流桥的工作原理
- JDBC (IV)
- JS function to realize simple calculator
- 固態硬盤開盤數據恢複的方法
- Flex flexible layout for mobile terminal page production
- Detailed summary of flex layout
- A brief talk on media inquiry
猜你喜欢
![[image fusion] image fusion based on MATLAB directional discrete cosine transform and principal component analysis [including Matlab source code 1907]](/img/a1/f7a35a04e180e89d7f2fdbf89c1160.jpg)
[image fusion] image fusion based on MATLAB directional discrete cosine transform and principal component analysis [including Matlab source code 1907]

Everything is an object

EL & JSTL (XIII)

Detailed summary of position positioning

In depth understanding of line height and vertical align

How to use the Magic pig system reinstallation master

SOC验证环境的启动方式

Difference between asemi high power FET and triode

February 20ctf record

Create an environment for new projects
随机推荐
Filter & listener (XIV)
Creation and use of MySQL index
Why does the SQL statement hit the index faster than it does not?
Laravel Vonage SMS sending
Mysql interactive_ Timeout and wait_ Timeout differences
ORA-00800: soft external error
Region of Halcon: generation of multiple regions (3)
Small sample learning data set
My IC journey - the growth of senior chip design verification engineers - "Hu" said that IC engineers are perfect and advanced
The print area becomes smaller after epplus copies the template
The construction and usage of wampserver framework
XSS (cross site script attack) summary (II)
Object creation and invocation code example
CTFHub-rce
Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
win11蓝牙无法连接怎么办?win11蓝牙无法连接的解决方法
epplus复制模板后打印区域变小的问题
Deeply understand the characteristics of standard flow and off standard elements
How do the defi protocols perform under this round of stress test?
What if the desktop computer is not connected to WiFi