0

This code is showing empty object ( {} )


// declared at top
 
let mainData = {};
let trainStations = {};
let routes = {};
let trainNo = {};

   data["data"].forEach(async (element) => {
   const response2 = await fetch(
   `https://india-rail.herokuapp.com/trains/getRoute?trainNo=${element["train_base"]["train_no"]}`
              );
   const data2 = await response2.json();
   data2["data"].forEach((ele) => {
   routes[ele["source_stn_code"]] = true;
   });
   trainNo[element["train_base"]["train_no"]] = routes;
    });
   console.log(trainNo);

if i do this then i will give response with data

   data["data"].forEach(async (element) => {
   const response2 = await fetch(
   `https://india-rail.herokuapp.com/trains/getRoute?trainNo=${element["train_base"]["train_no"]}`
              );
   const data2 = await response2.json();
   data2["data"].forEach((ele) => {
   routes[ele["source_stn_code"]] = true;
   });
   trainNo[element["train_base"]["train_no"]] = routes;
   console.log(trainNo);
    });

maybe there is some scooping issue please kindly help me to solve this problem :)

Dojeto
  • 1
  • 2
  • using async/await in a foreach loop can be kinda confusing sometimes (at least for me it is) i would refactor it in a way that may be easier to read https://stackoverflow.com/a/37576787/17097798 – Jordan Wright Nov 20 '22 at 07:38
  • 1
    @JordanWright it's not "confusing", it's (most of the time) just *wrong* because `forEach` does not support `await` ing async callbacks ... – derpirscher Nov 20 '22 at 08:11
  • Does this answer your question? [Using async/await with a forEach loop](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) – derpirscher Nov 20 '22 at 08:12

1 Answers1

-1

Please refer here and also check this.

As a short note, using await inside a forEach() loop will give unexpected results. This is because the forEach() does not wait until the promise to settled (either fulfilled or rejected).

A simple solution for this could be using either the traditional for loop or the for..of loop.

   for(let element of data["data"]){
      const response2 = await fetch(
      `https://india-rail.herokuapp.com/trains/getRoute?trainNo=${element["train_base"]["train_no"]}`
               );
      const data2 = await response2.json();
      data2["data"].forEach((ele) => {
      routes[ele["source_stn_code"]] = true;
      });
      trainNo[element["train_base"]["train_no"]] = routes;
   }
   console.log(trainNo);

NOTE: Make sure to wrap the above for..of loop inside an async function because the await keyword is allowed inside a function only when the function is defined with async keyword.

Soorya J
  • 145
  • 6
  • If you refer to other questions solving that problem please don't post an answer with links but close the question as a duplicate (I know you may not have the necessary reputation yet ...) – derpirscher Nov 20 '22 at 08:12
  • Okay @derpirscher, I really dont know about this, will correct this hereafter. – Soorya J Nov 20 '22 at 09:02