title: How to wait for function to finish in javascript?
body: <p>How to wait for function to finish in JavaScript?
  I have 2 functions <code>updateSeason</code> and <code>updateFixtures</code> and I want to wait for first one to finish before run next one.</p>
  <p>Both my functions are async and they working perfectly fine. Only problem is if I do not use <code>setTimeout</code> I need to run it twice because <code>updateFixture</code> run before <code>updateSeason</code> finish and there is still no file for fucntion to fetch on first run.</p>
  <p>updateData</p>
  <pre><code>const updateData = async () =&gt; {
    await updateSeason();
    await updateFixtures();
  };
  </code></pre>
  <p>updateSeason</p>
  <pre><code>    // UPDATE SEASON
  const updateSeason = async () =&gt; {
    // SEASONS TEMPLATE
    let seasonsTemplate = {
      timestamp: new Date(),
      season: null,
      leagues: [],
    };
  
    // FETCH LEAGUES INFO
    const leagues = await fetch(url + &quot;leagues?subscribed=true&amp;&quot; + key)
      .then((response) =&gt; response.json())
      .then((data) =&gt; data.data);
  
    // MAP THROUGH LEAGUES
    leagues.map(async (league) =&gt; {
      const id = `${league.league_id}&amp;`;
  
      // FETCH SEASONS INFO
      const seasons = await fetch(url + &quot;seasons?league_id=&quot; + id + key)
        .then((response) =&gt; response.json())
        .then((data) =&gt; data.data);
  
      // MAP THROUGH LEAGUES &amp; POPULATE SEASONS TEMPLATE
      seasons.map((season) =&gt; {
        if (season.is_current) {
          seasonsTemplate.season = `${moment(season.start_date).format(&quot;YYYY&quot;)}_${moment(season.end_date).format(&quot;YYYY&quot;)}`;
          seasonsTemplate.leagues.push({
            country_id: season.country_id,
            league_id: league.league_id,
            league_name: league.name,
            season_id: season.season_id,
            start_date: season.start_date,
            end_date: season.end_date,
          });
        }
      });
  
      // CHECK / CREATE SEASON FOLDER
      const currentSeasonFolder = `./data/${seasonsTemplate.season}`;
      if (!existsSync(currentSeasonFolder)) {
        await mkdir(currentSeasonFolder);
        await mkdir(`${currentSeasonFolder}/matches`);
      }
  
      // CREATE / UPDATE SEASON FILES
      await writeFile(&quot;./data/current_season.json&quot;, JSON.stringify(seasonsTemplate));
      await writeFile(`${currentSeasonFolder}/season.json`, JSON.stringify(seasonsTemplate));
  
      console.log(`${league.name} updated...`);
    });
  };
  </code></pre>
  <p>updateFixtures</p>
  <pre><code>    // UPDATE FIXTURES
      const updateFixtures = async () =&gt; {
        // FIXTURES TEMPLATE
        let fixturesTemplate = {
          timestamp: new Date(),
          season: null,
          fixtures: [],
        };
      
        // FETCH CURRENT SEASON INFO
        const season = await fetch(api + &quot;current_season.json&quot;).then((response) =&gt; response.json());
      
        // POPULATE FIXTURES TEMPLATE SEASON
        fixturesTemplate.season = season.season;
      
        // MAP THROUGH LEAGUES
        season.leagues.map(async (league) =&gt; {
          const id = `${league.season_id}&amp;`;
      
          // FETCH COMPETITION FIXTURES
          const fixtures = await fetch(url + &quot;matches?season_id=&quot; + id + key)
            .then((response) =&gt; response.json())
            .then((data) =&gt; data.data);
      
         
  
   // MAP THROUGH FIXTURES &amp; POPULATE FIXTURES TEMPLATE
      fixtures.map((match) =&gt; {
        if ((match.home_team.team_id === teamId || match.away_team.team_id === teamId) &amp;&amp; match.status !== &quot;postponed&quot;) {
          fixturesTemplate.fixtures.push({
            match_timestamp: new Date(match.match_start_iso).getTime(),
            match_start: match.match_start_iso,
            match_id: match.match_id,
            status: match.status === &quot;&quot; ? &quot;notstarted&quot; : match.status,
            home_team: getTeamName(match.home_team.team_id),
            home_short: getShortName(match.home_team.team_id),
            away_team: getTeamName(match.away_team.team_id),
            away_short: getShortName(match.away_team.team_id),
          });
        }
      });
  
      // SORT FIXTURES BY DATE IN ASCENDING ORDER
      fixturesTemplate.fixtures.sort((a, b) =&gt; a.match_timestamp - b.match_timestamp);
  
      // CREATE / UPDATE FIXTURES FILES
      const currentSeasonFolder = `./data/${season.season}`;
      await writeFile(currentSeasonFolder + &quot;/fixtures.json&quot;, JSON.stringify(fixturesTemplate));
  
      console.log(&quot;Fixtures updated...&quot;);
    });
  };
  </code></pre>
  <p><strong>UPDATE:</strong></p>
  <p>Issue was inside functions itself. <code>async Array.prototype.map</code> repleaced with <code>for</code> loop in both functions <code>updateSeason</code> and <code>updateFixtures</code> and now is working</p>