Function returning [ Promise { <pending> } ]

Asked

Viewed 116 times

1

Someone can help me, I’m with a function returning as pending, but do not know what can be already tried everything. follows my code:

  const episodes = seasons.map((el) => {
    const season = moviedb
      .seasonInfo({ language: language, id: tmdbId, season_number: el.season_number })
      .then((res) => {
        const meta = res.episodes.map((el, index) => {
            return {
              id: `${tmdbId}:${el.season_number}:${el.episode_number}`,
              name: `${el.name}`,
              season: `${el.season_number}`,
              number: `${el.episode_number}`,
              episode: `${el.episode_number}`,
              thumbnail: `https://image.tmdb.org/t/p/original${el.still_path}`,
              overview: `${el.overview}`,
              description: `${el.overview}`,
              rating: `${el.vote_average}`,
              firstAired: el.air_date,
              released: el.air_date,
            }
          })
        return meta
      })
      .catch(console.error);
    return season
  });
  console.log(await episodes)
  return episodes;
}
  • you are making a mess with the Law... I advise you to read more on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

  • instead of Season = moviedb.... then(). catch(), you should either use a await Movie.. without the then.catch, or about the entire function with a Promise and call the resolver()/Reject(), at the end of the chain from then,catch,e a await for this file, if the function is asynchronous...

  • You can use await thus const season = await moviedb, but the place you call also has to be async. The important thing is to understand how callbacks https://answall.com/q/45706/3635 works and to understand how Promises: https://answall.com/q/119907/3635

1 answer

0

Hi, I suspect it prints in the console.log what you want but returns in the function the wrong value, is that it? If this is the case it may be that returning the value of the Promise instead of the Promise itself is the problem. Exchange your last Return for:

return await episodes;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.