当前位置:网站首页>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);
})
边栏推荐
猜你喜欢

Web3 DAPP user experience best practices

【FLink】access closed classloader classloader. check-leaked-classloader

Detailed summary of float
![[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]

渗透测试-目录遍历漏洞

olap分析引擎——Kylin4.0

魔法猪系统重装大师怎么使用

buuctf(re)

Visual studio 2022 interface beautification tutorial

CTFHub-rce
随机推荐
Rce code execution & command execution (V)
Attack and defense world web baby Web
The SQL response is slow. What are your troubleshooting ideas?
Apache+php uploading large files
The construction and usage of wampserver framework
Array: force deduction dichotomy
Filter & listener (XIV)
In Net 6 using dotnet format formatting code
Difference between asemi high power FET and triode
olap分析引擎——Kylin4.0
本轮压力测试下,DeFi协议们表现如何?
Cookie & session & JSP (XII)
How to download and use Xiaobai one click reload on the official website
电脑的dwg文件怎么打开
ORA-00800: soft external error
Startup mode of SoC verification environment
Go deep into the working principle of browser and JS engine (V8 engine as an example)
XML (VIII)
What if the desktop computer is not connected to WiFi
Eyeshot Ultimate 2022 Crack By Xacker