Promise
前期准备
定义部分返回 Promise 对象的函数
```js
const p1 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ resule: 'access', code: 200, value: 'p1' })
}, 1000)
})
}
const p2 = () => {
return new Promise((_, reject) => {
setTimeout(() => {
reject({ resule: 'fail', code: 400, value: 'p2' })
}, 400)
})
}
const a1 = async () => {
await setTimeout(() => {}, 450)
return { result: 'success', code: 200, value: 'a1' }
}
const a2 = async () => {
await setTimeout(() => {}, 300)
throw new Error('fail a2')
}
const funSuccessObj = {
p1,
a1,
}
const funFailObj = {
p2,
a2,
}
const funObj = {
p1,
p2,
a1,
a2,
}
```
Promise.all
```ts
PromiseConstructor.all<Promise<unknown>[]>(values: Promise<unknown>[]): Promise<unknown[]>
```
当所有的promise都执行完且都返回resolve时,返回一个permise数组。若有返回了reject,则返回第一个reject
如果都为 resolve 则返回 resolve 数组,若有返回 reject 则 catch 返回第一个 reject
```js
const testPromiseAll = function () {
Promise.all(Object.values(funObj).map((item) => item()))
.then((res) => {
console.log('promise all has error then:'.padEnd(20, ' '), res)
})
.catch((err) => {
console.log('promise all has error catch:'.padEnd(20, ' '), err)
})
Promise.all(Object.values(funSuccessObj).map((item) => item()))
.then((res) => {
console.log('promise all successs then:'.padEnd(20, ' '), res)
})
.catch((err) => {
console.log('promise all successs catch:'.padEnd(20, ' '), err)
})
}
// promise all has error catch: Error: fail a2
// promise all successs then: (2) [{…}, {…}]
```
Promise.allSetled
```ts
PromiseConstructor.allSettled<Promise<unknown>[]>(values: Promise<unknown>[]): Promise<PromiseSettledResult<unknown>[]>
```
返回所有的结果数组,其中status为状态fulfilled|rejected,value为fulfilled的结果,reason为rejected的原因
返回 promise 结果数组 包含三个 属性 status: 状态fulfilled|rejected, value?: 状态为 fulfilled 的值;reason?: 状态为rejected的值
```js
const testPromiseAllSettled = function () {
fulfilled 的值;reason?: 状态为rejected的值
Promise.allSettled(Object.values(funObj).map((item) => item()))
.then((res) => {
console.log('promise allSettled then:'.padEnd(20, ' '), res)
})
.catch((err) => {
console.log('promise allSettled catch:'.padEnd(20, ' '), err)
})
Promise.allSettled(Object.values(funFailObj).map((item) => item()))
.then((res) => {
console.log('promise allSettled fails then:'.padEnd(20, ' '), res)
})
.catch((err) => {
console.log('promise allSettled fails catch:'.padEnd(20, ' '), err)
})
}
// promise allSettled fails then: (2) [{…}, {…}]
// promise allSettled then: (4) [{…}, {…}, {…}, {…}]
```
Promise.any
```ts
PromiseConstructor.any<Promise<unknown>[]>(values: Promise<unknown>[]): Promise<unknown> || xxx
```
返回第一个 resolve ,如果所有的结果都为 reject 则返回被catch捕获返回AggregateError: All promises were rejected
```ts
const testPromiseAny = function () {
Promise.any(Object.values(funObj).map((item) => item()))
.then((res) => {
console.log('promise any then:'.padEnd(20, ' '), res)
})
.catch((err) => {
console.log('promise any catch:'.padEnd(20, ' '), err)
})
Promise.any(Object.values(funFailObj).map((item) => item()))
.then((res) => {
console.log('promise any fails then:'.padEnd(20, ' '), res)
})
.catch((err) => {
console.log('promise any fails catch:'.padEnd(20, ' '), err)
})
}
// promise any then: {result: 'success', code: 200, value: 'a1'}
// promise any fails catch: AggregateError: All promises were rejected
```
Promise.race
```ts
PromiseConstructor.race<Promise<unknown>[]>(values: Promise<unknown>[]): Promise<unknown>
```
返回第一个 resolve ,如果所有的结果都为 reject 则返回被catch捕获返回AggregateError: All promises were rejected
```ts
const testPromiseRace = function () {
Promise.race(Object.values(funObj).map((item) => item()))
.then((res) => {
console.log('promise race then:'.padEnd(20, ' '), res)
})
.catch((err) => {
console.log('promise race catch:'.padEnd(20, ' '), err)
})
Promise.race(Object.values(funFailObj).map((item) => item()))
.then((res) => {
console.log('promise race fails then:'.padEnd(20, ' '), res)
})
.catch((err) => {
console.log('promise race fails catch:'.padEnd(20, ' '), err)
})
}
// promise race then: {result: 'success', code: 200, value: 'a1'}
// promise race fails catch: Error: fail a2
```