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);

猜你喜欢

转载自blog.csdn.net/xicc1112/article/details/104596306