1, let p1 = new Promise(); //=>Uncaught TypeError: resolver undefined is not a function 2, let p = new Promise([executor]); // [executor]: The executable function + new Promise is immediately executed within the Promise. The function is generally used to manage asynchronous programming code (it is possible to do this without asynchronous programming) + Pass two values to the [executor] function: resolve/reject P is an instance of the Promise class + the built-in private property [[PromiseState]] instance state: [[PromiseResult]] promise.prototype + then + catch + finally  + Symbol(Symbol.toStringTag): "Promise"Copy the code

Note: The [[]] is the browser’s built-in property, not for us to play with

3,

let p1 = new Promise((resolve, // This is a big pity. // This is a big pity. // This is a big pity resolve('OK'); [[PromiseState]]:fulfilled [[PromiseResult]]:'OK' // reject('NO'); [[PromiseState]]: Rejected [[PromiseResult]]:'NO' // [[PromiseState]]:reject // [[PromiseResult]]: reject('NO'); });Copy the code

4, state is synchronous5, asynchronous then

let p = new Promise((resolve, reject) => { console.log(1); resolve('OK'); Console. log(2); }); // console.log(p); // The status has been changed to success... / / execution p.t hen (onfulfilledCallback onrejectedCallback) / / + first passed in onfulfilledCallback and onrejectedCallback stored "is stored in a container: Because it is based on then to store a lot of a callback function "/ / + second again to verify the current state of the instance / / + if the instance status is pending, do not do any processing / / + if has become fulfilled/rejected, It tells the corresponding callback function to execute "but instead of executing immediately, it places it in a microtask queue in an EventQueue." "Promises themselves are not asynchronous, they manage asynchrony, P.chen (result => {console.log(' success -->', result); }); console.log(3);Copy the code

6. Asynchrony in promise: First, then perform asynchronous microtasks; Then resolve and reject; Resolve and Reject change the state of instances synchronously, but the methods in notify THEN perform asynchronous microtasks

let p = new Promise((resolve, reject) => { console.log(1); setTimeout(() => { resolve('OK'); Console.log (p); // + change the state and value of the instance to "synchronize" // console.log(4); }, 1000); //=> stores an asynchronous macro task console.log(2); }); console.log(p); P.chen (result => {console.log(' success -->', result); }); console.log(3); // Wait 1000ms, execute the timer function "take out the asynchronous macro task to execute"Copy the code

7,

Eight,

let p1 = new Promise((resolve, reject) => { resolve('OK'); // reject('NO'); }); Then (result => {console.log(' p1 successful -->', result); return Promise.reject(10); }, reason => {console.log('P1 failed -->', reason); }); // How did the state and value of p2 change? / / + regardless of execution, which is based on p1. Then stored onfulfilledCallback/onrejectedCallback either of the two methods / / + as long as the method does not report errors / / + instance if the method returns a new Promise, The success or failure of the "brand new Promise instance" determines the success or failure of P2 // + If it does not return a Promise? [[PromiseState]]: Fulfiled [[PromiseResult]]: Return value // + Then (result => {console.log(' console. successful ->', result); }, reason => {console.log('P2 failed -->', reason); return Promise.resolve(10); }); Then (result => {console.log(' console.log ', result); }, reason => {console.log('P3 failed -->', reason); return Promise.resolve(10); }); console.log(1);Copy the code

9, if you don’t pass onfulfilledCallback/onrejectedCallback, status, and the result will be postpone/through to the next callback function of the same status should be performed on “internal is actually added some implementation effect of the default function”

new Promise((resolve, reject) => { // resolve('OK'); reject('NO'); }).then(null /! *result=>result*! / , null /! * reason=>Promise.reject(reason) *! (/). Then the result = > {the console. The log (' success -- >, result); }, reason => {console.log(' failed -->', reason); }). Then (result = > {the console. The log (' success -- >, result); }, reason => {console.log(' failed -->', reason); });Copy the code

10. In general, we write “then” and “catch” at the end

new Promise((resolve, reject) => { resolve('OK'); // reject('NO'); }). Then (result = > {the console. The log (' success -- >, result); // console.log(a); }). Then (result = > {the console. The log (' success -- >, result); return Promise.reject('xx'); }). The catch (a tiny = > {the console. The log (' failure -- > '" reason); });Copy the code

Catch only deals with things that are in a failed state

Promise.prototype.catch = function (onrejectedCallback) {
    return this.then(null, onrejectedCallback);
}; 


Copy the code