0

Await function doesn't work for me, when the code reach to line 36 from call in line 22 (until this line everything work properly), the code jump to line 32 (doesn't execute lines (22-31)) , than go back to line 13 and return empty array.

Any idea what i am doing wrong?

/*1*/ async function getMatchesInfo(matches_ids_list){
/*2*/    let promises = [];
/*3*/    matches_ids_list.map((id) =>
/*4*/      promises.push(
/*5*/        axios.get(`${api_domain}/fixtures/${id}`, {
/*6*/          params: {
/*7*/            api_token: process.env.api_token,
/*8*/          },
/*9*/        })
/*10*/      )
/*11*/    );
/*12*/    let matches_info = await Promise.all(promises);
/*13*/    const matches_rel_info = await extractRelevantMatchData(matches_info);
/*14*/    return matches_rel_info
/*15*/  }
/*16*/
/*17*/
/*18*/  async function extractRelevantMatchData(matches_info) {
/*19*/    list = []
/*20*/    await matches_info.map( async (match_info) => {
/*21*/      const {time, id, localteam_id, visitorteam_id, venue_id}=match_info.data.data;
/*22*/      let stadium_info = await getStadiumName(venue_id) 
/*23*/      list.push({
/*24*/        match_id: id,
/*25*/        date: time.starting_at.date.split('-').reverse().join('/'),
/*26*/        hour: time.starting_at.time.split(':').slice(0,2).join(':'),
/*27*/        host_team: localteam_id,
/*28*/        away_team: visitorteam_id,
/*29*/        stadium: stadium_info,
/*30*/      })
/*31*/    })
/*32*/    return list
/*33*/ }
/*34*/
/*35*/  async function getStadiumName(venue_id){
/*36*/    const venue_info = await axios.get(`${api_domain}/venues/${venue_id}`, {
/*37*/      params: {
/*38*/        api_token: process.env.api_token,
/*39*/      },
/*40*/    })
/*41*/    return venue_info.data.data.city + ',' + venue_info.data.data.name;
/*42*/  }
/*43*/
/*44*/ (async()=>{return await getMatchesInfo([1,2,3])})
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 1
    Please don't use `.map` to iterate and then `.push` to an *outside* variable. Just return inside `map` and assign... – Yoshi May 19 '21 at 11:55
  • 1
    This might give you answer: [Use async await with Array.map](https://stackoverflow.com/questions/40140149/use-async-await-with-array-map). – Jax-p May 19 '21 at 11:57
  • @GuerricP The question depends on line numbers in the text. Your edit removed them, changed formatting, which in turn changed line numbers, and didn't add any alternative. – ASDFGerte May 19 '21 at 12:04
  • 1
    @ASDFGerte Hope this works. Made the line numbers comments, so the code is valid, at least. – VLAZ May 19 '21 at 12:06

0 Answers0