0

I am trying to get wanted data from API through the function and then let a = function to get value of data wanted.

let url = "https://itunes.apple.com/search?term=eminem&media=music&limit=2.";
function fetchData() {
    return fetch(url).then(function (response) {
        return response.json();
    })
        .then(function (json) {
            return json;
        });
}

fetchData().then(function (result) {
    console.log(result);
});

var a = fetchData().then(function (result) {
    console.log(result);
});
console.log(a);

console.log(result) inside the function shows correct data but I need it outside the function to work with it. As I am a beginner I got an idea to call the function inside variable then use that variable to work with data inside using for loop.

What am I missing?

Thank you!

Trying to get JSON value outside the function to work with data collected. I've read through a lot of answered questions on this problem but just got more confused so I am asking for precise instructions to see where I failed.

  • `a` will be a promise, not the result. Have a read of https://stackoverflow.com/questions/25530263/returning-a-value-from-a-promise – evolutionxbox Mar 16 '23 at 00:47

0 Answers0