0

Below is my code. I want to store the fetched JSON data in a variable outisde of the function scope. How can I do that? Also, if I want to run some functions on the fetched JSON data, am I supposed to do them inside .then()?

let jsondata;

fetch('https://api.myjson.com/bins/wa759')
  .then(function(response) {
    return response.json();
  })
  .then(function(myJson) {
    console.log(JSON.stringify(myJson));
    jsondata = myJson;
  });

  console.log("test");
  console.log(jsondata);

This logs

> Hello (corresponding to `console.log("test");`)
> undefined (corresponding to `console.log(jsondata);`)
> (the entire JSON data)
Leonard
  • 2,978
  • 6
  • 21
  • 42

0 Answers0