How to set a time interval between requests in Javascript?

Viewed 31

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)

0 Answers
Related