This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August

Problem scenario

Promise.all is processed concurrently when running asynchronously. This puts too much pressure on the server to respond to a number of high-volume XHR requests

demand

The specified number of concurrent tasks (1 or more) is not increased, unless new asynchronous tasks are started sequentially from the Promise task after the process ends

process

Before using

Use the plug-in to limit concurrency to 5

New tasks are started only when a specified number of tasks are completed

Plugin instructions

NPM address: www.npmjs.com/package/js-…

instructions

All, like promise.all, is actually a function that takes a Promises array and returns a Promise, limiting the number of concurrent asynchrony

Installation 0.
npm i js-asyncpool --save
Copy the code
1. The introduction of
import jsAsyncPool from 'js-asyncpool'

Copy the code
2. Parameter Description

jsAsyncPool.asyncPool(promiseArr,limit)

parameter role
promiseArr An array of promise objects
limit Several can be concurrent, and so on
Example 3.
import jsAsyncPool from 'js-asyncpool' let arr = [] function createPromise (j) { return ()=>{ return new Promise(resolve  => { console.log(`promise${j} start`); setTimeout(() => { console.log(`promise${j} over------`); resolve(); }, 3000); }) } } arr.push(createPromise(1)) arr.push(createPromise(2)) arr.push(createPromise(3)) arr.push(createPromise(4)) arr.push(createPromise(5)) arr.push(createPromise(6)) arr.push(createPromise(7)) console.log(arr,'----arr') jsAsyncPool.asyncPool(arr,3)Copy the code

  1. update
npm update js-asyncpool
Copy the code

Related knowledge points

First, the use of pomise.all

Common usage scenario: Multiple asynchronous results are merged together

Promise.all can wrap multiple Promise instances into a new Promise instance. Use to wrap multiple Promise instances into a new Promise instance.

1. It takes an array as an argument.

2. Arrays can be Promise objects or other values, and only promises wait for state changes.

3. When all child Promises are complete, the Promise is complete and returns an array of all values.

4. If any of the Promise fails, the return value is the result of the first failed child Promise.

The code example

Let p1 = new Promise((resolve, reject) => {resolve(' resolve ')}) let p2 = new Promise((resolve, reject)) Reject) => {resolve('success')}) let p3 = promse.reject (' failure ') Promise. (p2). Then (result) = > {the console. The log (result) / / [' success ', 'success'] }).catch((error) => { console.log(error) }) Promise.all([p1,p3,p2]).then((result) => { console.log(result) }). The catch ((error) = > {the console. The log (error) / / failed, hit 'failure'})Copy the code
The use of promise.race

This is similar to promise.all (), except that if either of these returns success, the process is completed but does not stop immediately

Common usage scenario: An asynchronous operation is combined with a timer. If the timer is triggered first, the operation times out and the user is notified

Let p1 = new Promise((resolve, reject) => {setTimeout(() => {resolve(' succeed ')}, 2000); let p1 = new Promise((resolve, reject) => {setTimeout(() => {resolve(' succeed ')}, 2000); }) let p2 = new Promise((resolve, reject) => { setTimeout(() => { resolve('success') }, 5000); }) Promise. Race (/ p1, p2). Then ((result) = > {the console. The log (result) / / [' success ', 'success'] }).catch((error) => { console.log(error) })Copy the code