“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”
This is the article I prepared for the Promise series
JS Promise Literacy and quick Start “up” – Nuggets (juejin. Cn)
JS Promise Literacy and quick Start “In” – Nuggets (juejin. Cn)
Combination of futures
Promise.all()
Receiving an iterable object Usually we pass an array, a single object within the iterated element, usually a Promise object created for new
Or, in this array, we pass the values directly, as shown:
Resolve object, which does not need to use promise.all, is written directly in sync code blocks
Promise.all is most commonly used to asynchronously load multiple unrelated data, and the callback function waits for all of that asynchronous data to be ready before executing
For example, when the background design API interface returns, id is used to replace nested objects, and the data of these nested objects already has a ready-made interface. Without modifying the background interface, we can directly pull these copies of data, complete the function of object nesting on the page, and then execute our own business
Promise. All normally waits for all promises in its array arguments to be resolved, and then handles the data passed in their resolution as an array object in the order that the arguments were passed in all(), wrapped as Promise. This may be a little convoluted, but look at the picture below:
However, if there is a rejected term in the Promise object in the array, promise. all returns the rejection as promise. reject.
Note that even if there is a rejected term, all other terms in the array that normally execute will still run as before!! In an effort to to
The rejected term is returned in the all method, and the reason for the rejected term has been returned. But ③ here, we still have an output after 2s
Promise.race()
The race method returns the first term that was processed, whether it was resolved or rejected.
Receives a regular array object of about the same length as all()
Promise.any()
The any method is a new feature and was recently supported by browsers, so use it with caution. The receive parameters are the same as all()
When the first term in the argument is resolved, the resolved result is wrapped as promise.resolve
The best use for this is that when we request a resource, there are several alternative channels, and we can immediately start the callback process after we successfully pull the resource in the first term