js implements axios limit request queue

js implements axios limit request queue

The background is:

In actual development, you may encounter network problems or a large number of queries, and the user may initiate the next request before the previous request is completed.

What will happen?

However, sending the same request multiple times to the server will undoubtedly put pressure on the server, so it is necessary to limit requests on the front end after optimizing the server query speed and when the user's network conditions are poor.

The cancelToken that comes with axios can help us achieve this requirement, and provides us with a ready-made api axios.CancelToken, which is a callback function whose return value is a request information. We can execute this callback function when we need to cancel. The specific implementation is as follows:

const service = axios.create({});
const penddingMap = new Map();
const addPendding = (config) => {
 config.cancelToken = config.cancelToken || new axios.CancelToken(cancel => {
  if (!penddingMap.has(config.url)){
   penddingMap.set(config.url,cancel);
  }
 })
}

const removePendding = (config) => {
 if (penddingMap.has(config.url)){
  let cancel = penddingMap.get(config.url);
    cancel(config.url);
    penddingMap.delete(config.url)
 }
}

A Map is maintained locally to store each request information. In addPendding, it will first determine whether there is a cancelToken. If there is, there is no need to recreate a cancelToken. In removePendding, it is determined whether the request information is in the Map. If the request exists in the Map, the cancel function is executed and the request in the Map is deleted.

Specific application in interceptor:

service.interceptors.request.use(config => {
 removePending(config) // If it exists in the Map, cancel the request first addPendding(config) // Add the request to the Map return config
})

service.interceptors.response.use(response => {
 ``` // some code
  return response.data
}, error => {
 // Capture cancel request and throw if(error instanceof Cancel){
    error.message = 'The previous request has not yet ended, please wait~';
    Message.error(error.message);
    return Promise.reject(error.response) 
  // Pay attention to the throw here, call try-catch to capture it when requesting})
)

Once the interception is successful here, multiple requests with large traffic can be limited.

This is just an example. You can also control whether the user initiates a request by judging the button logic.

This is the end of this article about js implementing axios limiting request queue. For more relevant js axios limiting request queue content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use Promise in JavaScript to control the number of concurrent requests
  • Sample code for using js to implement Ajax concurrent requests to limit the number of requests
  • gin Get JSON body of post request
  • JS implements request dispatcher
  • PHP implements the conversion of chrome form request data into json data used by the interface
  • Detailed explanation of several solutions for JavaScript interruption requests

<<:  A Brief Analysis of the Differences between “:=” and “=” in MySQL

>>:  How to use Docker-compose to build an ELK cluster

Recommend

Detailed explanation of ES6 Promise usage

Table of contents What is a Promise? Usage of rej...

MySQL query syntax summary

Preface: This article mainly introduces the query...

Summary of MySQL password modification methods

Methods for changing passwords before MySQL 5.7: ...

js dynamically implements table addition and deletion operations

This article example shares the specific code for...

MySQL performance optimization: how to use indexes efficiently and correctly

Practice is the only way to test the truth. This ...

Detailed example of changing Linux account password

Change personal account password If ordinary user...

Detailed steps to install nginx on Apple M1 chip and deploy vue project

brew install nginx Apple Mac uses brew to install...

Linux firewall status check method example

How to check the status of Linux firewall 1. Basi...

How to set up vscode remote connection to server docker container

Table of contents Pull the image Run the image (g...

Detailed explanation of 6 ways of js inheritance

Prototype chain inheritance Prototype inheritance...

Detailed tutorial on deploying Django project under CentOS

Basic Environment Pagoda installation service [Py...

CentOS 8.0.1905 installs ZABBIX 4.4 version (verified)

Zabbix Server Environment Platform Version: ZABBI...

Summary of Docker configuration container location and tips

Tips for using Docker 1. Clean up all stopped doc...

Detailed explanation of anonymous slots and named slots in Vue

Table of contents 1. Anonymous slots 2. Named slo...