简体   繁体   English

如何在 javascript 中等待 function 完成?

[英]How to wait for function to finish in javascript?

How to wait for function to finish in JavaScript?如何在 JavaScript 中等待 function 完成? I have 2 functions updateSeason and updateFixtures and I want to wait for first one to finish before run next one.我有 2 个函数updateSeasonupdateFixtures ,我想等待第一个函数完成后再运行下一个函数。

Both my functions are async and they working perfectly fine.我的两个函数都是异步的,它们工作得很好。 Only problem is if I do not use setTimeout I need to run it twice because updateFixture run before updateSeason finish and there is still no file for fucntion to fetch on first run.唯一的问题是,如果我不使用setTimeout ,我需要运行它两次,因为updateFixtureupdateSeason完成之前运行,并且在第一次运行时仍然没有要获取的文件。

updateData更新数据

const updateData = async () => {
  await updateSeason();
  await updateFixtures();
};

updateSeason更新季节

    // UPDATE SEASON
const updateSeason = async () => {
  // SEASONS TEMPLATE
  let seasonsTemplate = {
    timestamp: new Date(),
    season: null,
    leagues: [],
  };

  // FETCH LEAGUES INFO
  const leagues = await fetch(url + "leagues?subscribed=true&" + key)
    .then((response) => response.json())
    .then((data) => data.data);

  // MAP THROUGH LEAGUES
  leagues.map(async (league) => {
    const id = `${league.league_id}&`;

    // FETCH SEASONS INFO
    const seasons = await fetch(url + "seasons?league_id=" + id + key)
      .then((response) => response.json())
      .then((data) => data.data);

    // MAP THROUGH LEAGUES & POPULATE SEASONS TEMPLATE
    seasons.map((season) => {
      if (season.is_current) {
        seasonsTemplate.season = `${moment(season.start_date).format("YYYY")}_${moment(season.end_date).format("YYYY")}`;
        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("./data/current_season.json", JSON.stringify(seasonsTemplate));
    await writeFile(`${currentSeasonFolder}/season.json`, JSON.stringify(seasonsTemplate));

    console.log(`${league.name} updated...`);
  });
};

updateFixtures更新夹具

    // UPDATE FIXTURES
    const updateFixtures = async () => {
      // FIXTURES TEMPLATE
      let fixturesTemplate = {
        timestamp: new Date(),
        season: null,
        fixtures: [],
      };
    
      // FETCH CURRENT SEASON INFO
      const season = await fetch(api + "current_season.json").then((response) => response.json());
    
      // POPULATE FIXTURES TEMPLATE SEASON
      fixturesTemplate.season = season.season;
    
      // MAP THROUGH LEAGUES
      season.leagues.map(async (league) => {
        const id = `${league.season_id}&`;
    
        // FETCH COMPETITION FIXTURES
        const fixtures = await fetch(url + "matches?season_id=" + id + key)
          .then((response) => response.json())
          .then((data) => data.data);
    
       

 // MAP THROUGH FIXTURES & POPULATE FIXTURES TEMPLATE
    fixtures.map((match) => {
      if ((match.home_team.team_id === teamId || match.away_team.team_id === teamId) && match.status !== "postponed") {
        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 === "" ? "notstarted" : 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) => a.match_timestamp - b.match_timestamp);

    // CREATE / UPDATE FIXTURES FILES
    const currentSeasonFolder = `./data/${season.season}`;
    await writeFile(currentSeasonFolder + "/fixtures.json", JSON.stringify(fixturesTemplate));

    console.log("Fixtures updated...");
  });
};

UPDATE:更新:

Issue was inside functions itself.问题在于函数本身。 async Array.prototype.map repleaced with for loop in both functions updateSeason and updateFixtures and now is working async Array.prototype.mapupdateSeasonupdateFixtures函数中都替换for循环,现在正在工作

Why not just,为什么不只是,

async function updateData() {
  await updatedSeason();
  await updateFixtures()
}

updateData();

You can just use async/await for your defined functions, even if you cannot use 'await' keyword outside an async function.即使您不能在异步 function 之外使用“await”关键字,您也可以只对定义的函数使用 async/await。

So, for example, you have a index.js file, you can do:因此,例如,您有一个 index.js 文件,您可以执行以下操作:


async function main() {
  await updateSeason();
  await updateFixtures();
}

main()

or invoking directly the function with the short form或使用缩写形式直接调用 function


(async function main() {
  await updateSeason();
  await updateFixtures();
})()

Anyway, avoid to use 'writeFileSync' or other 'Sync' functions inside an async function because that block the event loop and decrease your performance.无论如何,避免在异步 function 中使用“writeFileSync”或其他“同步”函数,因为这会阻塞事件循环并降低性能。

EDIT: I saw now that you are using Array.prototype.map function with an async callback, that could be the problem.编辑:我现在看到您正在使用带有异步回调的 Array.prototype.map function,这可能是问题所在。

Try to look here: https://flaviocopes.com/javascript-async-await-array-map/试试看这里: https://flaviocopes.com/javascript-async-await-array-map/

Or, otherwise, use a standard for loop to handle your leagues或者,否则,使用标准 for 循环来处理您的联赛

You forgot to put "await" before the function您忘了在 function 之前加上“等待”

const updateData = async () => {
  // Add await before the functions because they are async functions
  await updateSeason()       
  await updateFixtures()
};

Async Await can help you out here since they are both async functions. Async Await 可以在这里为您提供帮助,因为它们都是异步函数。

const updateData = async () => {
  await updateSeason();
  await updateFixtures();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM