hay i making tons of requests to api, and i need to control the stream of the requests
i have function that using axios to make the requests and return a promises, and i going true the array and making the request, then i use Promise.all() to finish the requests, without any delay and control, where i can put some control functions to limit the requests before they sent to the api
that my get func:
return new Promise((resolve, reject) => {
console.log(`Getting address for ${lat}, ${lng}...`);
// Make sure we pass lat,lng to the API.
axios.get(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&result_type=locality&result_type=administrative_area_level_1&result_type=country&key=${apiKey}`)
.then(response => {
return resolve(response)
})
.catch(error => {
return reject(error.message)
}).catch(reject => {
return reject
})
})
}
then i going true the array and using the get func
const newData = data.map(async item => {
let results = ''
let reqStatus = ''
if (item.Longitude !== "blank") {
const req = await getAddress(item.Longitude, item.Latitude).catch((error) => {
console.log(error);
});
reqStatus = req.status
results = req.data
} else {
results = 'blank'
}
let newItem = ''
//console.log(JSON.stringify(results, null, 2))
console.log(`status: ${reqStatus} ${results.status}`)
if (reqStatus == '200' && results.status == 'OK') {
const rstlength = results.results[0].address_components.length
newItem = {
...item,
//results_from_google: results, //results.results[0].address_components[0].long_name
address_1: rstlength > 0 ? results.results[0].address_components[0].long_name : 'blank',
address_2: rstlength > 1 ? results.results[0].address_components[1].long_name : 'blank',
address_3: rstlength > 2 ? results.results[0].address_components[2].long_name : 'blank',
address_4: rstlength - 1 > 3 ? results.results[0].address_components[3].long_name : 'blank'
}
} else {
newItem = {
...item,
//results_from_google: results, //results.results[0].address_components[0].long_name
address_1: 'blank',
address_2: 'blank',
address_3: 'blank',
address_4: 'blank'
}
}
return newItem;
})
now how can i delay the requests?