-1

For example:

var obj = {};

$.getJSON('data.json', function(data) {
    obj = data;
});

console.log(obj)

The console then outputs an empty object instead of whatever value data was.

Alaanor
  • 37
  • 1
  • 7
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Erazihel Aug 11 '17 at 07:57

1 Answers1

2

in your code

$.getJSON('data.json', function(data) {
    obj = data;
});

is an asynchronous function, the code

console.log(obj)

does not wait for it to stop executing and hence you cannot see anything

marvel308
  • 10,288
  • 1
  • 21
  • 32