This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

promise

let p1 = new Promise([executor]);

  • The first thing to do when new is to execute the [executor] function

  • [executor] is passed two arguments, resolve and reject

  • Promise Status Status/Promise Value

    • New promise: status:pending value:undefined
    • Resolve function performs the status: fulfilled/resolved value: the first argument
    • The reject function executes status: Rejected Value: the first argument passed
    • Once the status changes to success or failure, the current operation is irreversible and the state is established and cannot be changed to any other state
    • Promise.prototype has three methods: then/catch/finally
    • Then Based on the THEN method, we can store 0 to 2 packets in the “event pool” corresponding to the current instance. If the promise state becomes successful, the first method in the THEN will be executed, and if the state is failed, the second method will be executed. Regardless of which method is executed, the value will be passed
Let p1 = new Promise((resolve,reject)=>{ resolve(10) },1000) }) p1.then(result=>{ console.log("result") },reason = >{ console.log("reason") })Copy the code
Let p1 = new Promise((resolve,reject)=>{setTimeout(()=>{resolve(10)},1000)}) The status of p2 instance succeeds or fails, and its value, is determined by the execution of the methods in the p1 event pool. 1 Regardless of whether p1 succeeds or fails, as long as the code does not throw an exception, etc., p2 will change to the status of success. If A/B executes without an error, but returns A new PROMISE instance, then wait for the new Promise instance to return A result. Resolve (100), promise.reject (200) return a successful or failed Promise instance let p2 = p1. Then (result=>{ console.log("result") },reason = >{ console.log("reason") })Copy the code

p1.then(result=>{}); p1.catch(reason=>{}) === p1.then(null,reason=>{})

p1.then(result=>{}).catch(reason=>{

})

If the corresponding method is missing from then, the state and method will be deferred to the next method in the same location and multiple promise instances will be stored in the array, Promise.all returns a new promise. let p3 = promise.all ([promise1,promise2]) p3.then(result=>{result array, }). Catch (reason=>{

})

Let p3 = promise.race ([promise1,promise2]) p3.then(result=>{

}).catch(reason=>{

})