Using async / await

Using async / await

https://jasonwatmore.com/post/2021/09/05/fetch-http-post-request-examples

https://openjavascript.info/2022/01/03/using-fetch-to-make-get-post-put-and-delete-requests/

The await operator is used to wait for a Promise. It can only be used inside an async function within regular JavaScript code;

Without using it:

function fetchMoviesHandler() {
  fetch('https://swapi.dev/api/films/')
    .then((response) => response.json())
    .then((data) => {
      const transformedMovies = data.results.map(movieData => (
        {
          id: movieData.episode_id,
          title: movieData.title
        }
      ));


      setMovies(transformedMovies);
    });

Using async and await

the best

async function fetchMoviesHandler() {
  const response = await fetch('https://swapi.dev/api/films/');
  const data = await response.json();
  const transformedMovies = data.results.map(movieData => (
    {
      id: movieData.episode_id,
      title: movieData.title,
      openingText: movieData.opening_crawl,
      releaseDate: movieData.release_date
    }

  ));
  setMovies(transformedMovies);
}