0

I have jQuery loading a json, and I am using .then() notation to run a function doThis() when the json is ready. However, if I pass a variable on doThis(), it returns 'undefined'.

I figured out this much: In my application the jQuery gets run on mouse click, and when passing a variable, it does run on second mouse click. Seems like the json gets loaded, but jQuery does not wait for the json to finish loading if I pass a variable.

This works:

$.getJSON('/endpoint/', function(data) {
  data = data
})
  .then(doThis)

This does not work even with no variable in the parenthesis:

$.getJSON('/endpoint/', function(data) {
  data = data
})
  .then(doThis())

I am sure this has something to do with the architecture of JavaScript. Would be awesome if someone could enlighten me a little.

Thanks in advance!

andrus
  • 39
  • 7

0 Answers0