Promise is a new method in ES6 that can be used to retrieve data asynchronously

There are three states in a promise, namely, Pendding, resolve and Reject. Once the promise state changes from pendding to resolve or REJECT, it cannot be changed later

The use of promise.all and promise.race in promises and their differences

promise.all

Use: Wrap multiple Promise instances into a new Promise instance;

Features: The data structure returned by success and failure is different, success returns an array, array elements corresponding to the promise incoming request, do not worry about the order problem caused by different request response speed; Failure returns the failure message of the request that was rejected first

Application scenario: the rendering of a module in the page depends on the comprehensive judgment of the request and response data of two or more interfaces to render. When all the requests are not returned, it is displayed in the form of loading. When all the interfaces return successfully, it is judged to render the specified data to generate the UI interface

promise.race

Race is a race that returns the fastest response data, whether the result itself is reject or resolve

Usage scenario: Multiple servers have the same server code deployed. If I want to get an item list interface, I can write the address of the item list interface of all servers in race. Whichever server responds quickly, I get the data from that server