参数化的异步函数返回Promise {“ _40”:0,“ _ 55”:“英语”,“ _ 65”:1,“ _ 72”:null,}

如何从承诺对象中摆脱出“英语”

异步功能->参数化

const getLanguageNamefromCode = async (lng_code) => {
  let response, langData, lang;
  try {
    response = await fetch(
      `https://restcountries.eu/rest/v2/lang/${lng_code}?fields=languages`
    );
    langData = await response.json();
    // console.log(langData[0].languages[0]);
    if (langData[0].languages[0].iso639_1 === lng_code) {
      lang = langData[0].languages[0].name;
      console.log(lang);
    }
    return lang;
  } catch (err) {
    throw new Error(err);
  }
};

从for循环内部调用它并发送参数
      const loadedTrendingMovies = [];
      const loadedTrendingMoviesLength = trendingMovies.results.length;

      for (let i = 0; i < 6; i++) {
        // let credits;
        hasUserSaved = getState().UserMovies.userMovies.find(
          (userMovie) =>
            userMovie.id === trendingMovies.results[i].id.toString()
        );

        loadedTrendingMovies.push(
          new Movie(
            trendingMovies.results[i].id.toString(),
            trendingMovies.results[i].media_type === "movie"
              ? trendingMovies.results[i].title
              : trendingMovies.results[i].name,
            posterBaseUrl + trendingMovies.results[i].poster_path,
            trendingMovies.results[i].media_type === "movie"
              ? trendingMovies.results[i].release_date
              : trendingMovies.results[i].first_air_date,
            [],
            trendingMovies.results[i].overview,
            trendingMovies.results[i].vote_average,
            getLanguageNamefromCode(trendingMovies.results[i].original_language),
            hasUserSaved ? hasUserSaved.location : ""
          )
        );
      }

如何从getLanguagefromCode异步方法返回的承诺中获取值。

评论