Some noticeable points about Promise object

By studying Ruan Yifeng's Getting started with ECMAScript 6 , some knowledge points that Promise can easily forget

new Promise((resolve, reject) => {
    reject()
    resolve()
    console.log('hello')
}).then(() => {
    console.log('resolve');
}, () => {
    console.log('reject');
})
for (let i = 0; i < 100; i++) {
    console.log('hi');
}

/*output
hello
hi
hi
...
hi
reject

Promise will be executed as soon as it is created. The callback functions resolve and reject will not execute until all synchronization tasks of the current script have been executed, and only one of them can be executed in promise. Calling resolve or reject will not terminate the execution of promise function, and the subsequent console.log('hello ') will execute and print out first, because the immediate callback function is at the end of the current event cycle The last execution is always later than the synchronization task of this cycle.

let p1 = new Promise((resolve, reject) => {
    reject()
    resolve()
})

let p2 = new Promise((resolve, reject) => {
    resolve(p1)
}).then(() => {
    console.log('resolve');
}, () => {
    console.log('reject');
})

/*output
reject

The resolve method of p2 returns p1. Because p2 returns another Promise, p2's own state is invalid, and p1's state determines p2's state. Therefore, the later then statements become for the latter (p1). When the parameter of the resolve or reject function is a Promise object, the state of the parameter determines the state of the current Promise object

Promise can use the then() method for chained programming to solve the problem of callback to hell, and the structure is clearer.

new Promise((resolve, reject) => {
    resolve('hello')
}).then((str) => {
    console.log('resolve', str);
    return new Promise((resolve, reject) => {
        resolve('world')
    })
}).then((str) => {
    console.log('resolve', str);
    return new Promise((resolve, reject) => {
        reject('nihao')
    })
}).then((str) => {
    console.log('resolve', str);
}, (str) => {
    console.log('reject', str);
})

/*output
resolve hello
resolve world
reject nihao

When the return value of the callback function is a Promise object, it can be programmed in a chain way. Obviously, the state of the Promise object has no influence on each other, that is, the state of the next callback function is determined by the newly returned Promise object

The internal error of Promise will not affect the external code of Promise, that is to say, the internal error of Promise will not terminate the process and continue to execute the program

new Promise((resolve, reject) => {
    resolve(x + 2)
}).then(val => {
    console.log('resolve', val);
}).catch(err => {
    console.log('Error:' + err);
})

setTimeout(() => {
    console.log('123');
}, 1000)

/*output
//Error: ReferenceError: x is not defined
123

Note: if an asynchronous operation in the Promise instance object throws an error, the process will terminate as follows

new Promise((resolve, reject) => {
    resolve(2)
    setTimeout(() => {
        throw new Error('err')
    }, 1000)
}).then(val => {
    console.log('resolve', val);
}).catch(err => {
    console.log('Error:' + err);
})
setTimeout(() => {
    console.log('1111111');
}, 2000)

/*output
resolve 2
Error: err  //Process termination

In the above code, Promise specifies to throw an error in the next "event loop". At that time, the operation of Promise is over, so this error is thrown outside the Promise function, and it will bubble to the outermost layer, becoming an uncaught error.

Published 10 original articles, won praise 0, visited 146
Private letter follow

Keywords: ECMAScript Programming

Added by bigswifty on Wed, 29 Jan 2020 17:30:45 +0200