0

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...

Kim
  • 15
  • 8
  • `.map()` does not understand `async` callbacks, so the result of the `.map()` will be an array of Promise objects. Your code has to `await` that array with `Promise.all()` or some other similar solution. – Pointy Jun 22 '22 at 12:37
  • Also, as posted your `.map()` callback does not have a `return` so you'll end up with an array of `undefined`. – Pointy Jun 22 '22 at 12:38

2 Answers2

0

Maybe you should do something like this :

    let promises = arr.map((each,i)=>{ return es.getData(each.indexElastic, each[0].attributes.field_query) });

    let result = await Promise.all(promises).then(res => {
        return res.flat(1);
    });
    console.log(result);

Don't forget to put this into an async function ;)

nem0z
  • 1,060
  • 1
  • 4
  • 17
  • I did ```result.then(res=>res.map((answer,i)=>auxJson.queries[i][`results${i}`] = answer))```, and that doesn't work my json doesn't have the new key with the results... – Kim Jun 22 '22 at 13:15
  • This is whatI get ```Promise { }``` if i make a ```console.log(result)``` , thanks for your help. – Kim Jun 22 '22 at 13:21
  • Oh ,it's my bad I miss read my code, I will update this – nem0z Jun 22 '22 at 13:28
  • I'm in an infinite loop returning the Promise XD – Kim Jun 22 '22 at 13:30
  • Can you share the result you get from this code ? – nem0z Jun 22 '22 at 13:37
  • My problem was that I didn't write an await, the code has too many async and awaits, sorry and thank you for your help. – Kim Jun 23 '22 at 15:46
0

You could do something like :

let results = await Promise.all(
  arr.map(elem => queryDB(elem))
)
JeanJacquesGourdin
  • 1,496
  • 5
  • 25
  • You of course meant `Promse.all()`. – Keith Jun 22 '22 at 12:53
  • No, because Promise.all shuffles the returned array depending on the time used to query the ressource. So using Promise.allSettled you can retreive which is which using the index of the first array and avoid some shenanigans. – JeanJacquesGourdin Jun 22 '22 at 12:55
  • `Promise.all()` does [preserve order](https://stackoverflow.com/a/28066851/5648954), there is no shuffling. – Nick Parsons Jun 22 '22 at 12:57
  • I did result.then(res=>res.map((answer,i)=>auxJson.queries[i][`results${i}`] = answer)), and that doesn't work my json doesn't have the new key with the results... – Kim Jun 22 '22 at 13:24
  • I think you are using a way too complex syntax to do this.. What is the output of console.log(results) when you use my answer : let results = await Promise.all( arr.map(elem => queryDB(elem)) ) ? – JeanJacquesGourdin Jun 22 '22 at 15:20