Add both settimeout and timeout to in the array loop

59 Views Asked by At

I am looping the array using bluebird map method and then using each row as a payload to my apicall function. Everything works perfectly but I want to incorporate timeout method when response from the api takes more than 10 seconds and also settimeout method to delay 2 seconds after each api call. Please let me know how can I acheive this. I am fine without using bluebird. Thanks in advance.

handleSubmit = () => {
   Promise.map(this.props.data, row => {
       return apiCall(api, row).then((response) => {       
           if(response){
               console.log(response)                
           } else{
               console.log("failed")
           }
       })
   }, { concurrency: 1 } )
}

apiCall: (api, input ) => {
   switch (process.env.NODE_ENV) {
     case 'production': { // Production environment
       return new Promise((resolve) => {
         window.runApi(api, input, (response) => {
           if (typeof response === 'string') {
             const jsonResponse = JSON.parse(response);              
             resolve(jsonResponse);
           } else {
             resolve(response);
           }
         });
       });
     }
     default: {         
       const requestBody = input;
       if (input !== "") {
         requestBody.username = "user";
         requestBody.password = "password";
       }
       const requestUrl = `api`;
       return fetch(requestUrl, {
         method: 'POST',
         headers: {
           'Content-Type': 'application/json',
         },          
         body: JSON.stringify(requestBody),          
       })
       .then(res => res.json());      
     }
   }
 }
0

There are 0 best solutions below