当前位置:网站首页>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);
})
边栏推荐
- olap分析引擎——Kylin4.0
- Kotlin compose perfect todo project surface rendering background and shadow
- XML (VIII)
- Visual studio 2022 interface beautification tutorial
- Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
- Detailed summary of position positioning
- For in JS Of and for in
- Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
- Web3 DAPP user experience best practices
- Get to know the drawing component of flutter - custompaint
猜你喜欢

Personalized Federated Learning with Moreau Envelopes

What if win11 Bluetooth fails to connect? Solution of win11 Bluetooth unable to connect

Heavy broadcast | phase shift method + mathematical principle derivation of multi frequency heterodyne + implementation

Eyeshot Ultimate 2022 Crack By Xacker

TX Text Control 30.0 ActiveX

Why is the TCP handshake just 3 times?

Rce code execution & command execution (V)

以太网是什么要怎么连接电脑

File upload vulnerability (III)

dotnet-exec 0.4.0 released
随机推荐
Page electronic clock (use js to dynamically obtain time display)
ThinkPHP 5 log management
Eyeshot Ultimate 2022 Crack By Xacker
Write shell script error summary
A review of small sample learning
Mobile number regular expression input box loses focus verification
PHP uses JWT
Drag modal box
EL & JSTL (XIII)
Swift rapid development
In Net 6 using dotnet format formatting code
How to make colleagues under the same LAN connect to their own MySQL database
Rce code execution & command execution (V)
MySQL concept and operation (III)
融合CDN,为客户打造极致服务体验!
Activereportsjs V3.0 comes on stage
PHP calls map API
OOP vector addition and subtraction (friend + copy construction)
Summary of SQL injection (I)
Huawei Hongmeng development lesson 4