当前位置:网站首页>面试会问的 Promise.all()
面试会问的 Promise.all()
2022-07-02 04:35:00 【庇耳拉海莱】
Promise.all()
来看这个的应该都在面试吧~~~
Promise.all() 接收一组可迭代的 Promise 作为输入, 返回一个新的 Promise 对象, 其 resolve 回调的结果是输入的所有 Promise 的结果组成的数组.
如果输入的参数为空的可迭代对象, 或者所有输入的 Promise 都变成已完成状态, 那么返回的 Promise 也会变成已完成状态.
const promise1 = new Promise(resolve => {
setTimeout(() => {
resolve('');
}, 1000);
});
const promise2 = Promise.resolve(2);
const promise3 = 1;
Promise.all([promise1, promise2, promise3])
.then((result) => {
console.log('result is', result);
});
// 空的可迭代对象
Promise.all(new Set())
.then(result => {
console.log('new Set() result is', result);
});

虽然 promise1 最后才变成已完成状态, 但是在返回的结果中, 它仍然是第一个. 返回结果中的位置和其完成先后没关系, 和其在传入 all() 的位置有关系
不过一旦输入的任何 Promise 对象变成 rejected 或者输入的参数非可迭代对象(比如 null 或者 number 类型),那么返回的 Promise 立即 reject, 并且 reject 回调的结果是第一个 reject 的信息或者报错信息.
const promise4 = new Promise(resolve => {
setTimeout(() => {
resolve(100);
}, 10000);
})
const promise5 = Promise.reject(200);
Promise.all([promise4, promise5])
.then(result => {
console.log('我不会执行的');
})
.catch(err => {
console.log('糟糕, ', err)
})

输入
Promise.all() 接收的是可迭代对象, 而不仅仅是数组, 数组虽然是可迭代对象, 但是 set, string 等都属于可迭代对象. 因此 Promise.all('asd') 会输出什么呢?
哈哈, 这是因为 asd 字符串类型可迭代的. 如果我们组成可迭代对象的数据(a, s, d)不是 Promise 对象, 就会被 resolve 变成已完成状态.
手写 Promise.all
面试中问到最多的就是被手写 Promise.all 实现
- 首先: 判断传入的参数是否为可迭代对象, 关于可迭代对象判断, 请看MDN这里;
- 其次, 如果是可迭代对象, 判断是否为空,
string和数组通过length属性判断,Set和Map通过size属性判断; - 创建两个变量, 分别使用
resolvedPromiseCount和resolvedPromiseResult用来记录已经完成的Promise对象的数量和结果; - 使用
Promise.resolve()处理可迭代对象中每个值, 如果这个值刚好是Promise对象,resolve就会原封不动地将这个对象返回; 如果这个值非Promise对象, 就会创建一个resolve这个值的Promise对象.- 在
catch方法中直接reject; - 在
then方法中增加resolvedPromiseCount的值并将已完成的Promise对象的结果保存在数组resolvedPromiseResult的对应位置. 如果resolvedPromiseCount刚好等于length调用resolve返回. 
- 在
Promise.myAll = function (iterable) {
return new Promise((resolve, reject) => {
if (typeof iterable[Symbol.iterator] !== 'function') {
reject(new Error('参数必须是可迭代类型对象'));
}
let length = iterable.length || iterable.size;
if (length === 0) {
resolve([]);
}
let resolvedPromiseCount = 0;
let resolvedPromiseResult = [...' '.repeat(iterable.length)]
let i = 0;
for (let item of iterable) {
Promise.resolve(item)
.then(result => {
resolvedPromiseCount++;
resolvedPromiseResult[i++] = result;
if (resolvedPromiseCount === length) {
resolve(resolvedPromiseResult);
}
})
.catch(err => {
reject(err);
});
}
})
}

谢谢你看到这里, 祝福你面试顺利
边栏推荐
- 66.qt quick-qml自定义日历组件(支持竖屏和横屏)
- 社交媒体搜索引擎优化及其重要性
- Target free or target specific: a simple and effective zero sample position detection comparative learning method
- [JS -- map string]
- How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
- 阿里云polkit pkexec 本地提权漏洞
- Gin framework learning code
- WiFi 5GHz frequency
- Idea autoguide package and autodelete package Settings
- Ognl和EL表达式以及内存马的安全研究
猜你喜欢

Common sense of cloud server security settings

Play with concurrency: what's the use of interruptedexception?

Keil compilation code of CY7C68013A

FAQ | FAQ for building applications for large screen devices

Introduction to vmware workstation and vSphere

win11安装pytorch-gpu遇到的坑

Pit encountered in win11 pytorch GPU installation

unable to execute xxx. SH: operation not permitted

Deep understanding of lambda expressions

Alibaba cloud polkit pkexec local rights lifting vulnerability
随机推荐
[C language] Dynamic Planning --- from entry to standing up
Major domestic quantitative trading platforms
Wechat applet pull-down loading more waterfall flow loading
Research on the security of ognl and El expressions and memory horse
6月书讯 | 9本新书上市,阵容强大,闭眼入!
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
June book news | 9 new books are listed, with a strong lineup and eyes closed!
Mysql中常见的锁
Unit testing classic three questions: what, why, and how?
Recyclerview add header
PIP installation of third-party libraries
How much is the tuition fee of SCM training class? How long is the study time?
cookie、session、tooken
Federal learning: dividing non IID samples according to Dirichlet distribution
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
MySQL advanced SQL statement 2
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
unable to execute xxx. SH: operation not permitted
Li Kou interview question 02.08 Loop detection