当前位置:网站首页>Handwritten promise series - all
Handwritten promise series - all
2022-06-27 16:58:00 【Mediocre °】
Handwritten questions are indispensable during the interview , and promise Related handwritten questions are often asked , Let's tidy up here promise Handwriting series .
The entire series is expected to have handwritten articles for the following functions , Let's start today all function
- all
- any
- race
- allSettled
- resolve+reject
- promise Handwritten implementation
Promise.all effect
Promise.all() Method is used to add more than one Promise The example is packaged into a new Promise example .
const a = Promise.all([p1, p2, p3])
Final a The status of the p1,p2,p3 decision . Only the states are fulfilled when a The state of is fulfilled, here ,p1,p2,p3 The return value of consists of an array , Pass to p Callback function for . Just one for rejected,a The state of will become rejected, At this time, the first was rejectd The return value of the strength of will be passed to a Callback function for . If the array passed in is not promise Instance value or object , Will be encapsulated as a state of fulfilled State of promise, The return value is itself .
Combined with the above characteristics , Let's write a few examples , Test it Promise.all Results in various cases
let promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('promise1')
resolve(1)
}, 2000)
})
let promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('promise2')
resolve(2)
}, 1000)
})
let promise3 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('promise2')
reject('error')
}, 3000)
})
let b = Promise.all([promise1, promise2, {
a: 2}, promise3])
console.log(b) // The direct output is peding State of Promise
// Output after three seconds
console.log(b)
/** Promise {<rejected>: 'error'} [[Prototype]]: Promise [[PromiseState]]: "rejected" [[PromiseResult]]: "error" */
let c = Promise.all([promise1, promise2, {
a: 2}])
console.log(c)
/** Promise {<fulfilled>: Array(3)} [[Prototype]]: Promise [[PromiseState]]: "fulfilled" [[PromiseResult]]: Array(3) 0: 1 1: 2 2: {a: 2} length: 3 [[Prototype]]: Array(0) */
Handwritten implementation Promise.all
Promise.myAll = (promises) => {
return new Promise((resolve, reject) => {
let result = []
promises.forEach((promise, i) => {
// Will all not promise The object is encapsulated as fulfilled State of promise object , about promise Objects are not processed
Promise.resolve(promise).then((res) => {
result[i] = res
if(i === promises.length - 1){
resolve(result)
}
}).catch((err) => {
reject(err)
})
})
})
}
let b = Promise.myAll([promise1, promise2, {
a: 2}, promise3])
let c = Promise.myAll([promise1, promise2, {
a: 2}])
console.log(b)
console.log(c)
// Results and all The same function
边栏推荐
- QT5 之信号与槽机制(信号与槽的基本介绍)
- 事务的隔离级别详解
- tensorflow求解泊松方程
- Qt5 signal and slot mechanism (demonstrate the correlation between the control's own signal and slot function)
- LACP details
- 郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
- QT5 之信号与槽机制(演示控件自带的信号与槽函数关联)
- Cloud security daily 220216: root privilege escalation vulnerability found on IBM SaaS integration platform needs to be upgraded as soon as possible
- 深耕数字化,引领云原生,服务更多开发者
- When the publish / subscribe mode encounters NET
猜你喜欢

List to table
P.A.R.A 方法在思源的简易应用(亲测好用)

2022年中国音频市场年度综合分析

d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll

【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)

Raspberry pie preliminary use
![[multithreading] thread communication scheduling, waiting set wait(), notify()](/img/4b/b9312fafd1ef1bf438f7aec7665276.png)
[multithreading] thread communication scheduling, waiting set wait(), notify()

10分钟掌握mysql的安装步骤

Ping An technology's practice of migrating from Oracle to ubisql

Handling method of occasional error reporting on overseas equipment
随机推荐
C language set operation
[fxcg] today's market analysis
Halcon: discrete digital OCR recognition
Performance problems caused by redis cache invalidation and competitive loading
2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
Oracle concept II
A large number of missing anchor text
Source NAT address translation and server mapping web page configuration of firewall Foundation
Solving Poisson equation by tensorflow
模拟进程调度
Related configuration commands of Huawei LACP
Mode setting of pulseaudio (21)
Oracle concept II
Raspberry pie preliminary use
Qt5 signal and slot mechanism (demonstrate the correlation between the control's own signal and slot function)
事务的隔离级别详解
LACP details
如何提升IT电子设备效能管理
QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
3.4 fixed number of cycles II