I have script like this below.
It start multiple API access.
var temp = [];
for (let i = 0; i < `10;i++){
var url = `http://www.myapi.com/api/?id=${i}`;
axios.get(url).then(res =>{
temp[i] = res['data'];
//I want to do something if every API get the results.
}).catch(res => {
console.log("axios error");
});
}
Now I want to wait every 10 api access finished.
Is it possible or how can I do this?
You could use Promise.all to wait for multiple requests,
Promise.all([
fetch('https://example.ex/articles'),
fetch('https://example.ex/authors')
]).then((responses) => {
// JSON object from each of the responses
return Promise.all(responses.map((response) => {
return response.json();
}));
}).then((data) => {
// You would do something with both sets of data
console.log(data);
}).catch((error) => {
console.log(error);
});