Promise

Once the state is set, it cannot be modified

Promise has three states: Pending, FULFILL, and Reject

Only operations within the object determine the result of a promise. The state can only be transferred from Pending to FULFILL or pending to Reject, and the state is irreversible

Promise.resolve(), promise.reject () returns a frozen Promise object promise.then ().catch().finally() promise.then () contains two callback functions, Return success and failure result callback promise.catch () return failure result callback promise.finally () final status result callback promose.race () parentheses hold an array of Promise objects, This method returns the result of the first completed object in the array, which can be used to determine the timeout Promise. All () holds an array of Promise objects in parentheses, This method returns either the successful result of the promise objects or only the first failure result. Promise.allsettled() stores the Promise array and returns the results of the Promise objects, whether settled or settledCopy the code

Promise by hand?

Promisify function

Promise modification of the callback function

function promisify(f) { return function () { let args = Array.prototype.slice.call(arguments); return new Promise((resolve, reject) => { args.push((err, res) => { if(err) { reject(err) } else { resolve(res) } }); f.apply(null, args) }); }}Copy the code

async/await

generator