I my case i have a array of objects
let arr = [
{
data: "data1"
},
{
data: "data2"
},
{
data: "data3"
}
];
i wanted to make a call in loop like this:
arr.forEach(() => {
axios.post(url, el.data).then(res => {
console.log(res.data);
});
})
.catch(err => console.log(err));
which in this case is looping through array and sending all request like machine gun and then they are coming back individually.
What i want is to make first call (first item in array) than wait for response when it comes back correct or error no matter just response. When i get it. I want to make second etc. as long as array has object init.
i found something like this but i do not understand if it will do
const axios = require('axios');
function makeRequestsFromArray(arr) {
let index = 0;
function request() {
return axios.get('http://localhost:3000/api/' + index).then(() => {
index++;
if (index >= arr.length) {
return 'done'
}
return request();
});
}
return request();
}
makeRequestsFromArray([0, 1, 2]);