Promise的学习笔记2

1. 抛出异常也可改变Promise的状态

除了resolve(value)和reject(reason)可以改变promise的状态以外,通过抛出异常也可以将promise状态由pending变为rejected。

        const p = new Promise((resolve, reject) => {
            throw 0 // throw new Error('0')
        })
        p.then(
            value => {},
            reason => {
                console.log('reason:', reason); //reason: 0
            }
        )
2. promise.then()

then()指定的回调函数执行的结果决定了promise.then()返回的新的promise的结果状态。

        new Promise((resolve, reject) => {
            resolve(1)
            // reject(1)
        }).then(
            value => {
                console.log('onResolved1()', value); // 1. onResolved1() 1
                // 2. return 2
                // 3. return Promise.resolve(3)
                // 4. return Promise.reject(4)
                // 5. throw 5
            },
            reason => {
                console.log('onReject1()', reason);
            }
        ).then(
            value => {
                console.log('onResolved2()', value); // 1. onResolved2() undefined
                // 2. onResolved2() 2
                // 3. onResolved2() 3
            },
            reason => {
                console.log('onReject2()', reason);
                // 4. onReject2() 4
            }
        )
3. then的链式调用串联多个同步/异步任务
new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('异步任务1')
                resolve(1)
            }, 1000);
        }).then(
            value => {
                console.log('任务1结果:', value)
                console.log('同步任务2')
                return 2
            }
        ).then(
            value => {
                console.log('任务2的结果:', value)
                promise.resolve(3)
            }
        ).then(
            value => {
                console.log('任务3的结果:', value)
            }
        )
        // 异步任务1
        // 任务1结果: 1
        // 同步任务2
        // 任务2的结果: 2
        // 异步任务3
        // 任务3的结果: 3
4. promise的链式穿透(一层层穿)

then链式调用时,前面任何操作出现异常,都会传到最后失败的回调中处理。

        new Promise((resolve, reject) => {
            // resolve(1)
            reject(1)
        }).then(
            value => {
                console.log('onResolved1()', value)
                return 2
            }
        ).then(
            value => {
                console.log('onResolved2()', value)
                return 3
            }
        ).then(
            value => {
                console.log('onResolved3()', value)
            },
            reason => Promise.reject(reason)
        ).catch(
            reason => {
                console.log('onRejected1()', reason);
                // 1. onRejected1() 1
                // 2. 
                return new Promise(() => {}) //中断promise链
                // 2. onRejected1() 1
            }
        ).then(
            value => {
                console.log('onResolved4()', value)
                // 1. onResolved4() undefined
            },
            reason => {
                console.log('onRejected2()', reason);
            }
        )
5. 常规来说,先指定回调函数,后改变状态

若想先改变状态,后指定回调函数可以

  1. 在执行器里面直接调用resolve()/reject() 【同步】
  2. 加一个setTimenout,延长更长时间再调用then()
        const p = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(1)
            }, 1000);
        })
        setTimeout(() => {
            p.then(
                value => {console.log('value:', value)},
                reason => {console.log('reason:', reason)}
            )
        }, 3000);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值