0

I'm trying to create a simple web scraper:

var url = "sadasd";

var obj;

fetch(url)
  .then(res => res.json())
  .then(data => obj = data)
  .then(() => console.log(obj))

My question is, why can't I access the obj variable outside the fetch with something like console.log(obj); ?

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
freemanfl
  • 55
  • 6
  • 1
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – evolutionxbox Aug 11 '21 at 09:27
  • It's likely because any code that's run that needs `obj` needs to _wait_ until `obj` has a value. – evolutionxbox Aug 11 '21 at 09:28

0 Answers0