0

i have a code that needs many different promises to solve before proceding with some computation. just for info, the promises are concerning accessing database.

for example:

      const house= await database.getTheHouse();
      const  person= await database.getThePerson();
      const  address= await database.getTheAdress();
      ...
      N-data= await database.getNData();

      compute(house,person,address)

so I wanted something like this, to increase the performance launching asynchronous but i have problems with the types of the array

listPromises=[ database.getHouse(), database.getPerson(),database.getAdress(), ...];
[house,person,address]= await Promise.all(listPromises);

compute(house,person,address);

then now i'm trying to do a forEach on the listPromises, but not sure what is the best approach, if any has found the same issue and has any advice, would be great.

let house;
let person;
let ...;
listOfFieldsNeeded.foreach(async element =>{ 
     switch (element){
            case House:
                 house= await database.getHouse();
                 break;
            case Person:
                 person= await database.getPerson();
                 break;
            ...
          }
})


...but here in the code, i cant use person because is used before asignation :/
  • Please do be aware that syntax in those last two lines won't work like you think: you will need a semicolon either at the end of the declaration of `listPromises` or prior to the destructuring assignment on the next line. If you can't recite from memory exactly how ASI works and what the edge-cases are, I recommend that you just add the bleeping semicolons. – Jared Smith Jul 13 '22 at 13:49
  • Ask **one** question at a time. Preferably one that hasn't already been asked here, and yes we expect you to search first. You asked a question, I gave you a link that answered it, then you edited to also ask a *completely different question* (which had *also* already been asked numerous times here). Please take the site tour, and read the help center about how to ask good questions. – Jared Smith Jul 13 '22 at 17:42

0 Answers0