0

I'm fetching the a movie api and adding the movie to the movie_db object. And after-all I have to return the movie_db changed.

const movie_db = [
    {
        genre: "comedy",
        movies: [{},{}]
    },
    {
        genre: "thriller",
        movies: [{},{}]
    },
    {
        genre: "horror",
        movies: [{},{}]
    },
    ]

function transformDB(movieOb){
        let dbIndex = movie_db.findIndex(e => e.genre === movie_db["genre"]);   
        var arr = [];
        arr.push(movie_db[dbIndex]);    
        if (dbIndex === -1){
            movie_db.push({
                genre: movieOb.Genre,
                movies: movieOb.Title
            });
            return movie_db;
        }
}

function letterboxd() {
        let movieQuest = prompt(`what is your favorite movie?`);
        let movieOb;

        const getMovieAsync = async movieQuest =>{
            let myMovie = await fetch(`http://omdbapi.com?t=${movieQuest}&apikey=thewdb`);
            movieOb = await myMovie.json();
            return  transformDB(movieOb);
        }
        return getMovieAsync(movieQuest).then((data) => data);
}

letterboxd()

the problem is that the return of the getMovieAsync is returning a promise and not a result even resolving it. i tried to solve it in different ways, but it wasn't being synchronous and the return wasnt waiting for the fetch/promises to be resolved. Which is the best way to make it synchronous and return the result correctly?

  • Since `getMovieAsync` is async, `letterboxd` can only return its result asynchronously, making it async as well. – deceze Jan 10 '21 at 14:04
  • which means that it will return a promise if I put the letterboxd func as async. is there a way to make it return the result? – Gabriella Tavares Jan 10 '21 at 14:16
  • It will always by necessity return a promise. No way around it. Read the [linked duplicate](https://stackoverflow.com/a/14220323/476). – deceze Jan 10 '21 at 14:26

0 Answers0