0

I want to extract an array from the public API, and all I'm receiving is a promise. How can I return the array from the API, outside my function?

Please have a look at the code I'm trying to implement:

var getMov = function() {
    return fetch('http://localhost:8080/api/projects')
        .then((response) => response.json())
        .then((responseJson) => {
            JSON.parse(responseJson.movies);
            return responseJson.movies;
        })
};
console.log(getMov());

Let me know if you have any ideas, how to solve this promise issue.

Vadim Badea
  • 409
  • 2
  • 8
  • 21

1 Answers1

1

You just have to use the then method again.

getMov().then(function(movies) {
    // do smth with movies
});

According to the promises specs the argument of the onFulfilled function will be the returned value from the last exectuted onFulfilled method, which is your case return the movies.

zoom
  • 1,686
  • 2
  • 16
  • 27