1

I am trying to get data from one endpoint and use it to hit another endpoint before I send the data to my frontend. I have tried a number of different async await combinations but the res.status.json always resolves before I manage to get the getAirport function to resolve. Any ideas on how I can get it to wait?

router.get("", (req, res, next) => {
  sabre
    .get("/v1/lists/supported/cities")
    .then(response => {
      let cities = response.data.Cities;
      let citiesObj = cities.map(city => {
        //console.log('here');
        return {
          name: city.name,
          code: city.code,
          airports: city.Links.map(link => {
            return getAirport(link["href"]);
          })
        };
      });
    
      res.status(200).json({ message: "ok", data: citiesObj });
    })
    .catch(err => console.log(err));
});

function getAirport(link) {
  let updatedUrl = link.replace(baseUrl, "");
  return sabre.get(updatedUrl);
}
https://stackoverflow.com/questions/ask#
Pari Baker
  • 696
  • 4
  • 19
  • 1
    Once again read about how to `async` works. `getAirport` is asynchronous, so you'll have to add couple `await`s. and https://stackoverflow.com/questions/40140149/use-async-await-with-array-map – bato3 Mar 19 '19 at 17:46
  • also `let citiesObj = await sabre.get("/v1/lists/supported/cities")` or move `res.send` into `then` – bato3 Mar 19 '19 at 17:48
  • yes I've tried nesting multiple awaits, maybe my sequencing wasn't right, as for using promise.all as the link recommends, the problem I'm having is that citiesObj is not the promise waiting to be resolve, citiesObj is an array containing cities {name:'',code:'', airports:[array of promises]}; – Pari Baker Mar 19 '19 at 17:55

1 Answers1

0

Code made "the feel" in a Notepad, so consider it as a hint rather than a solution.

router.get("", (req, res, next) => {
  sabre
    .get("/v1/lists/supported/cities")
    .then(async response => {
      let cities = response.data.Cities;
      let citiesObj = await Promise.all(cities.map(async city => {
        //console.log('here');
        let airports = await Promise.all(city.Links.map((link) => return getAirport(link["href"]);))
        return {
          name: city.name,
          code: city.code,
          airports: airports 
        };
      }));

      res.status(200).json({ message: "ok", data: citiesObj });
    })
    .catch(err => console.log(err));
});
  Working Solution: 

router.get("", (req, res, next) => {
  sabre
    .get("/v1/lists/supported/cities")
    .then(response => {
      let cities = response.data.Cities;
      let citiesObj = cities.map(async city => {
        let airports = await Promise.all(city.Links.map((link)=>{
       
          return getAirport(link['href'])
        })).then(response=>{
        
          return response;
        });
        return {
          type: 'city',
          name: city.name,
          code: city.code,
          transport:airports
        };
      });
Promise.all(citiesObj).then(cities=>{
  let response = cities.map(cities=>cities);
  res.status(200).json({message:'ok', data: response});

})
    })
    .catch(err => console.log(err));
});
function getAirport(link) {
  let updatedUrl = link.replace(baseUrl, "");
  return sabre.get(updatedUrl).then(response=>{
    return response.data;
  });
bato3
  • 2,695
  • 1
  • 18
  • 26
  • couple of tweaks on the function and it worked thanks – Pari Baker Mar 19 '19 at 19:08
  • @PariBaker If you can, please fix my code in answer. Let others have a solution, not a hint. :) – bato3 Mar 19 '19 at 22:17
  • sorry maybe I wasn't clear I meant a change to my function, although maybe it wasn't necessary. I'll check it at a later time. I will go ahead and post the full answer. Thanks – Pari Baker Mar 20 '19 at 15:06
  • @PariBaker thanks :) BTW: you shouldn't need `.then(response=>{ return response; })` because `await` resolves all promises chain. – bato3 Mar 20 '19 at 16:27
  • Yes I realized that after, but for some reason I was getting inconsistent results. Sometimes my front end would crash out because the object wasn’t getting resolved. So I’ll have to do a couple more runs to see why – Pari Baker Mar 20 '19 at 16:28