【问题标题】:Async function, do something after the map function is finishedasync函数,map函数完成后做一些事情
【发布时间】:2021-08-11 20:02:52
【问题描述】:
async function testing(summoner_name) {
  try {
    var match;
    let summoner = {
      name: [summoner_name],
    };
    const id = await fetchAccountID(summoner_name);
    const matchList = await fetchMatches(id);
    Object.keys(matchList.matches).map((key, i) => {
      setTimeout(async function () {
        match = await fetchMatch(matchList.matches[key].gameId); 
        summoner = await getMatchStats(
          match,
          matchList.matches[key].champion,
          summoner
        );
      }, i * 100);
    });
  } catch (error) {
    console.log(error);
  }
}

我想在 map 函数遍历所有键后做一些事情,我该如何实现?

【问题讨论】:

    标签: javascript async-await promise


    【解决方案1】:

    你是这个意思吗?

    async function testing(summoner_name) {
      try {
        let summoner = {
          name: [summoner_name],
        };
        const id = await fetchAccountID(summoner_name);
        const matchList = await fetchMatches(id);
        //Promise in Serial
        for (const key of Object.keys(matchList.matches)) {
          const match = await fetchMatch(matchList.matches[key].gameId);
          summoner = await getMatchStats(
            match,
            matchList.matches[key].champion,
            summoner
          );
        }
      } catch (error) {
        console.log(error);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 2023-03-06
      • 1970-01-01
      • 2017-10-13
      • 2021-07-15
      相关资源
      最近更新 更多