个人笔记 Promise方法使用

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
```
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值