I want to make ten api calls to populate an array. If i make the same api call but outside of the for loop i don’t get any problem. But doing it inside the for loop i get some succesfull requests and a ton with a 503 status code, service unavaiable.
Here is my js:
function generatePersonJson() {
const promise = axios.get(url, {
headers: {
Accept: "application/json",
"User-Agent": "axios 0.21.1",
},
});
const dataPromise = promise.then((response) => response.data);
return dataPromise;
}
const createPerson = async () => {
generatePersonJson()
.then((data) => {
if (data) {
return data
}
})
.catch((err) => console.log(err));
};
const createPersons = async () => {
let personsArray = [];
for (let i = 0; i < 11; i++) {
const dev = await createPerson();
personsArray.push(dev);
}
console.log(personsArray);
};
createPersons();
I get the error in the catch block inside the createPerson and it looks like this: response: { status: 503, statusText: 'Service Unavailable',