当前位置:网站首页>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);
})
边栏推荐
- Object creation and invocation code example
- CSRF (Cross Site Request Forgery) &ssrf (server request forgery) (IV)
- 固态硬盘开盘数据恢复的方法
- Prototypical Networks for Few-shot Learning
- 渗透测试-目录遍历漏洞
- CTFHUB SSRF
- Array: force deduction dichotomy
- 2021-10-24
- olap分析引擎——Kylin4.0
- Region of Halcon: generation of multiple regions (3)
猜你喜欢
随机推荐
Page electronic clock (use js to dynamically obtain time display)
融合CDN,为客户打造极致服务体验!
投资理财产品的年限要如何选?
Native JS high risk reminder pop-up code snippet, "are you sure you want to do this?" and "it cannot be recovered after deletion. Do you want to continue“
The print area becomes smaller after epplus copies the template
Svg code snippet of loading animation
Swift rapid development
[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear
Visual studio 2022 interface beautification tutorial
epplus复制模板后打印区域变小的问题
Triangle class (construction and deconstruction)
电脑的dwg文件怎么打开
Five simple data types of JS
SRC platform summary
XSS (cross site script attack) summary (II)
Mysql interactive_ Timeout and wait_ Timeout differences
Google Earth Engine(GEE)——全球JRC/GSW1_1/YearlyHistory数据集的批量下载(中国区域)
Penetration information collection steps (simplified version)
Route parameters to jump to the page and transfer parameters -- > hidden parameter list
Use js to simply implement the apply, call and bind methods



![H5 canvas drawing circle drawing fillet [detailed explanation]](/img/6f/a33a323b6cd0918066e8b71a22d841.jpg)





