0

I am developing a react application that performs a POST with axios to the api that makes the update in the database.

To do this, I store all the states to be updated in an array and with the .map function I make a post request for each object in the array. All good so far. The problem is that the api can only receive 3 requests per second.

Any idea how to set a time between each request ?

From the front I call my function with the data for the request.

            arrayLeadId.map(async function (leadId) {
                const formattedData = formatData(leadId, finalPipeline, finalStatus, user, tag);
              
                await pipelineServices.updateLeads(formattedData, token).then(resp => console.log(resp));
            });

pipelineServices.updateLeads(formattedData, token).then(resp => console.log(resp)); is the function that must be executed a maximum of 3 times per second.

updateLeads esta definida de la sigueinte forma dentro de PipelineServices

    async updateLeads(leads, token) {

        let config = {
            method: 'POST',
            url: `http://localhost:3525/api/updateLead`,
            data: {leads, token},
            headers: {}
        };
    
        let aux = await axios(config)
            .then(function (response) {
                if (response.status === 200) {
                    console.log("OK");
                    console.log(response);
                    return response.data.res; 
                }
                if (response.status === 401) {
                    console.log("User is not authorized");
                    return response; 
                }
                if (response.status === 400) {
                    console.log("Invalid data given. Details are available in the request response");
                    return response; 
                }
            })
            .catch(async function (error) {
                console.log("Error");
                console.log(error)
            });

        return aux;
    
    }

Any idea how to set a time between each request ? (i am starting in javascript)

Motias
  • 89
  • 9
  • 1
    You can use [axios-rate-limit](https://www.npmjs.com/package/axios-rate-limit) – Sergey Sosunov Sep 14 '22 at 12:52
  • See https://stackoverflow.com/questions/8682622/using-setinterval-to-do-simplistic-continuous-polling – James Sep 14 '22 at 12:52
  • Obviously - setTimeout is the shortest way. But if it's a simulation of 100 users, do we need to do something with the backend? If it's not a simulation, you can put all arrays in a request and return massive responses, this will run much faster than 100 requests. – Gibloor Sep 14 '22 at 13:49
  • Obviously i try the Obvious. axios-rate-limit is a very simple solution. – Motias Sep 14 '22 at 14:05

0 Answers0