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?