- Part I
so I have the following code =>
let array = [];
await objElements.forEach( async(element) => {
let results = await axios.get('backend_route', {params: element});
results.data.forEach(result => {
array.push(result);
console.log(result);
console.log(array);
// here the `result` variable is printed with the desired value and the array shows all the `result` singles values on it
});
});
console.log(array);
// But here, like if I would like to return the array at the end, it is empty :(
This is on JavaScript ES6 btw, if you have any possible solution any help is appreciated, thanks!
- Part II
Thanks for answering the previous question, I have been reading from Promises and still cannot figure out the new problem I have, so the code goes as following =>
const promise = array.map(async(item) => {
// here for each `item` I'm doing a call to the backend.
let response = await axios.get('route_to_backend', {params: item});
console.log(response);
/*When I try to get the response from the backend, it always send me
the response for the last `item` of the array, example: if the
array is [12, 67, 95, 06], it will make the request to the backend 4
times (length of the array) and always with the last item of the
array (in this example `06`) :(*/
});
//Finally as per the example I was provided
await Promise.all(promise);
Thanks a lot for your answers (Y)