To batch 100,1000, or more requests, you can use promise.all, but how do you know the progress of the interface during the request?
/** * simulates an asynchronous request */
function timeout(time){
return new Promise((resolve) = >{
setTimeout((a)= >{ resolve(time); },time); })}function allWithProgress(requests, callback){
let index = 0;
requests.forEach(item= >{
item.then((a)= >{
index ++;
const progress = index*100/arr.length; callback(progress); })});return Promise.all(requests);
}
const arr = [timeout(1000),timeout(2000), timeout(3000), timeout(4000), timeout(5000),timeout(6000),timeout(7000), timeout(8000), timeout(9000), timeout(10000)];
allWithProgress(arr, function(progress){
console.log(progress);
}).then(res= >console.log(res))
Copy the code
application
To CodePen
The reference reading: stackoverflow.com/questions/4…