I have a object I am iterating through and making an axios call per iteration. The following two bits of code are what I have attempted. In one I am using an async
function, and using await
-- In the other, I am using a promises
array and using Promise.all(promises)
to iterate. They both have the same outcome .. The axios call is sending out 1,000 plus calls all at once, and not "waiting" or "pausing". What am I not seeing?
This is my async
version.
function importProjects() {
Object.keys(importFiles).map(async function (key) {
await api.post(apiUrl() + "api/v2/addPin/",
{pin: importFiles[key]})
.then(response => {
console.log(response.data);
});
});
}
This is my Promise version.
function importProjects() {
let promises = [];
Object.keys(importFiles).map(function (key) {
promises.push(
api.post(apiUrl() + "api/v2/addPin/", {
pin: importFiles[key]
})
);
});
Promise.all(promises)
.then(response =>
console.log(response.data)
);
}
Why to both versions send all my requests simultaneously?