Promise 2: Key Questions

1. How to change the state of the promise ?

(1) resolve(value): If it is currently pending , it will become resolved
(2) reject(reason): If it is currently pending , it will become rejected
(3) An exception is thrown : if it is currently pending , it will become rejected

2. A promise specifies multiple success / failure callback functions , will they all be called ?

Called when the promise changes to the corresponding state
let p = new Promise((resolve, reject) => {
            // resolve('OK');
        });

        ///指定回调 - 1
        p.then(value => {
            console.log(value);
        });

        //指定回调 - 2
        p.then(value => {
            alert(value);
        });

3. Who should change the promise state and specify the callback function first ?

(1) It is possible . Under normal circumstances, the callback is specified first and then the state is changed, but it is also possible to change the state first and then specify the callback (when the task in the executor is a synchronous task, directly execute resolve or reject to change the state, and then use the then method specified callback)

(2) How to change the state first and then specify the callback ?

① Call resolve()/reject() directly in the executor
② Delay longer before calling then()
        let p = new Promise((resolve, reject) => {
                resolve('OK');
        });

        p.then(value => {
            console.log(value);
        },reason=>{
            
        })


        // 先执行回调,再改变状态

        let p = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('OK');
            }, 1000);
        });

        p.then(value => {
            console.log(value);
        },reason=>{
            
        })
(3) When will the data be obtained (when will the callback function be executed) ?
① If the callback is specified first , then when the state changes , the callback function will be called to get the data
② If the state is changed first , then when the callback is specified , the callback function will be called to get the data

4. What determines the result state of the new promise returned by promise.then() ?

(1) Simple expression : determined by the execution result of the callback function specified by then()
(2) Detailed expression :
① If an exception is thrown , the new promise becomes rejected, reason is the thrown exception
② If any non- promise value is returned , the new promise becomes resolved, and the value is the returned value. If there is no return value, the new promise value becomes resolved, and the value of the output promise is undefined at this time
③ If another new promise is returned , the result of this promise will become the result of the new promise

5. How does promise chain multiple operation tasks ?

(1) promise 's then() returns a new promise, which can be opened into a chain call of then()
(2) Multiple synchronous / asynchronous tasks are connected in series through the chain call of then
        let p = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('OK');
            }, 1000);
        });

        p.then(value => {
            return new Promise((resolve, reject) => {
                resolve("success");
            });
        }).then(value => {
            console.log(value);
        }).then(value => {
            console.log(value);
        })

// promise success 
// undefined  
//.then的确返回promise对象,但这个对象的值由回调返回的值决定,这里没有声明返回值,所以返回undefined,那么下一个then获取到的值就是undefined,直接打印出来

6. Promise abnormal transmission ?

(1) When using promise 's then chain call , you can specify the failed callback at the end ,
(2) Any exception in the previous operation will be passed to the last failed callback for processing
        let p = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('OK');
                // reject('Err');
            }, 1000);
        });

        p.then(value => {
            // console.log(111);
            throw '失败啦!';
        }).then(value => {
            console.log(222);
        }).then(value => {
            console.log(333);
        }).catch(reason => {
            console.warn(reason);
        });

// 失败啦

 7. Broken promise chain ?

(1) When the then chain call of promise is used , it is interrupted in the middle , and the subsequent callback function is no longer called
(2) Method : Return a promise object in the pending state in the callback function
        let p = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('OK');
            }, 1000);
        });

        p.then(value => {
            console.log(111);
            //有且只有一个方式
            return new Promise(() => {});
        }).then(value => {
            console.log(222);
        }).then(value => {
            console.log(333);
        }).catch(reason => {
            console.warn(reason);
        });
 // 111

Guess you like

Origin blog.csdn.net/csdssdn/article/details/126130422