Sharing in the form of personal notesCopy the code
Why use asynchronous programming?
Avoid blocking thread execution while waiting for an asynchronous operationCopy the code
What problem did Promise solve?
The problem of "callback hell" in traditional asynchronous programming is solved, that is, asynchronous operations are queued through Promise chained calls instead of function nestingCopy the code
About creating a Promise instance
PromiseThis instance is created in three states: Pending, resolved/fulfilled and RejectedPromiseExample:1.new Promise(executor) // You must pass an executor(executor function) as an argument, otherwise a SyntaxError will be raised
2.Promise.resolve() // Generate a completed Promise instance
3.Promise.reject() // Generate an instance of Promise that rejects the state and throws an asynchronous error. Note: Asynchronous errors cannot be caught by try/catch and can only be caught by asynchronous constructsAbout the actuator function: main function: initializationPromiseAsynchronous behavior and control state transitions1.throughnewcreatePromiseThe executor function is executed immediately as synchronous code2.Call resolve(), which switches to complete, and reject(), which switches to reject3.Once the state is set, it cannot be changed and a result is returned (value for completion or reason for rejection).let p = new Promise((resolve, reject) = > {
return resolve(); / / or reject ();
})
Copy the code
Promise
// Each of the following methods returns a new Promise instance
1.Promise.prototype.then() on parameters: accepts up to two parameters: The onResolved handler and the onRejected handler are executed in the corresponding completed or rejected states. Both arguments are optional, but if passed to then() any non-function type arguments will be ignored about the return value: if no return value is displayed, it will passPromise.resolve() wraps the default return valueundefinedIf there is a return value displayed, it will passPromise.resolve() wraps the value and returns it2.Promise.prototype.catch() is a syntax candy equivalent toPromise.prototype.then(null, onRejected), executed in the rejected state3.PromiseThe.prototype.finally() argument is an onFinally handler that executes in either the complete state or the rejected state, but there is no way to know which state returns the value, usually the parentPromisePassing of instances4.Promise.all() must pass an iterable as an argument, otherwise a syntax error will be thrown; If the iterable is empty, it is equivalent toPromise.resolve() if each of thePromiseIf instances are complete, return as long as there is one instance of the complete state (whose internal value is an array containing the values of each iterable in iterator order)PromiseIf an instance is a rejected state, return an instance of the rejected state (the reason is the first reason in the iterable to convert to the rejected state)5.Promise.race() must pass an iterable as an argument, or else throw syntactic errors to the first transition statePromiseInstance that wraps its completed internal value or reason for rejection and returns a new onePromiseThe instanceCopy the code
What’s wrong with promises?
1.Once the newPromiseInstance it executes immediately and cannot be canceled2.Once the state is set, it cannot be changed or viewedPromiseExecution progress of the instanceCopy the code
Here’s another interview question to help understand Promise:
const p = () = > new Promise((res, rej) = > {
console.log(3);
let f = new Promise((res, rej) = > {
console.log(7);
setTimeout(() = > {
console.log(5);
res(6);
}, 0);
res(1);
})
res(2);
f.then(arg= > {
console.log(arg);
});
});
p().then(arg= > {
console.log(arg);
});
console.log(4);
Copy the code