In a map I'm asigning the results of an async/await function from a db query, but I'm doing something wrong because it doesn't work, my code:
arr.map(async(each,i)=>{
await es.getData(each.indexElastic, each[0].attributes.field_query).then(res=>auxJson.queries[i][`results${i}`] = res)
It supose to generate a json like this:
{
queries:[
{results0: res},
{results1: res},
{results2: res},
...
]
}
The response is ok, inside the map all is good, but outside the map nothing is assigned to the auxJson. I also try with Promise.all and assign everything in the then() of the Promise.all.
What is wrong?
I did this:
let promises = arr.map((each,i)=>{ return es.getData(each.indexElastic, each[0].attributes.field_query) });
let result = Promise.all(promises).then(res => {
return res.flat(1);
});
result.then(res=>res.map((answer,i)=>auxJson.queries[i][`results${i}`] = answer))
And also doesn't work...