-1

I have googled and googled and can't seem to get out of this wet paper bag I find myself in.

I have an exported function that grabs a bunch of data from a web api, then we do some stuff to that data, go back and grab a little more data, and then finally return a javascript object back. At least that's the plan. I have everything working right up to the point where I have to send the data back from the module into my main app.js file.

Here is what I hope to be the relevant code from my function...there is lots I omitted, but I don't think it's relevant.

//SNIP

   let airportBoards = [...results];

   const updateBoards = () => {

       airportBoards.forEach(airport => {

           console.log(airport)
           boardTypes.forEach(board => {

               let currentOffset = airport[board].next_offset;

               (async function () {

                   while (currentOffset > 0) {

                       await getOffsetData(airport.airport, board, currentOffset)   //axios request
                       .then(res => {
                           currentOffset = res.data.AirportBoardsResult[board].next_offset
                           newFlights = res.data.AirportBoardsResult[board].flights
                           console.log(currentOffset)
                           let index = airportBoards.findIndex(r => r.airport === airport.airport)
                           airportBoards[index][board].flights.push(...newFlights)
                       })
                   }
               })()
           })
       })
   }

Ultimately what I need to do is get the contents of the "airportBoards" back out of this. Everything is working as far as I can tell, it loops through the offsets, and calls until there is no more data to return. And I can see the airportBoards array is updating properly using the debugging tools...I just can't figure out how to use it (airportBoards) once all the async/await stuff is settled.

Apologies in advance...I know its not pretty...I just hope I have provided enough information here for someone to give me a hand.

Thanks in advance!

pilotGuy
  • 111
  • 1
  • 10
  • [here](https://stackoverflow.com/a/64165144/3729184) is an answer I gave a couple of days ago about getting value from `async/await` . Maybe it can help you out – Omri Attiya Oct 03 '20 at 23:14
  • Thanks for this...I think I am already doing this for the most part. The issue is I need to know where to "return airportBoards" to actually get all the values out. Everything I've tried returns "airportBoards" before the async function has completed. Maybe I need another .then, just not sure where it would go.... – pilotGuy Oct 03 '20 at 23:22

1 Answers1

1

You can use Promise.all to wait for all promises to resolve:

let airportBoards = [...results];

const updateBoards = () => {
  let arrayOfPromises = [];
  airportBoards.forEach(airport => {
    console.log(airport)
    boardTypes.forEach(board => {
      let currentOffset = airport[board].next_offset;
      let promise = (async function() {
        while (currentOffset > 0) {
          await getOffsetData(airport.airport, board, currentOffset)
            .then(res => {
              currentOffset = res.data.AirportBoardsResult[board].next_offset
              newFlights = res.data.AirportBoardsResult[board].flights
              console.log(currentOffset)
              let index = airportBoards.findIndex(r => r.airport === airport.airport)
              airportBoards[index][board].flights.push(...newFlights)
            })
        }
      })();
      arrayOfPromises.push(promise);
    });
  })
  Promise.all(arrayOfPromises).then(() => { /* finish - do whatever you want */ });
}
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35
  • I have tried this, and while it gets me farther, I still am getting undefined when I try to return airportBoards. I have Promise.all(arrayOfPromises).then(() => { console.log(airportBoards); return airportBoards } Then in my app.js file I call the function : let airportBoards = getFAIdByAirport(airports,'ga','arrivals') airportBoards.then(res => { console.log(res) }) This logs "undefined" – pilotGuy Oct 04 '20 at 00:38
  • Ya, so I'm an idiot. This of course is the way. I broke it all down and started over using this principal, and it works great. Who knows where I was going wrong before. Thanks! – pilotGuy Oct 04 '20 at 14:33