I've been struggling for hours to understand what I'm doing wrong in the following:
This is a function that takes as an argument an array exampleArr
:
const mapOverItemsArray= async (exampleArr) => {
let resultObj= {};
await Promise.all(exampleArr.map(async item=> {
await timeout(1000);
let result;
try{
const response = await axios.get(`...api/${item}`);
result= processResponse(response)
}catch(e){
console.log(e);
};
resultObj= {
...resultObj,
[item]: result
};
})
);
return resultObj;
};
It is intended to await for a function timeout
, that returns a promise , because the API has /second usage limits:
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
But it does not seem to wait, which I can tell by the error messages - namely too many requests. Additionally, this is how I call the mapOverItemsArray
:
//... enclosing async function
await mapOverItemsArray(exampleArr);
//...rest
Thanks a lot !