-2

I've tried to read a Json file with loadJSON from p5 but the data comes to late. If I try console.log(JSON.stringify(data)) it says {}.

function setup() {
    for(i = 0;i <= 3;i++) {

        //load the JSON
        data = loadJSON('test'+ i +'.json');

        //Work with it
        console.log(JSON.stringify(data);
   }
}
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
MrMugame
  • 15
  • 4
  • can you post the code that is giving you this issue – Nick Parsons Dec 02 '18 at 09:46
  • 1
    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) – Alon Eitan Dec 02 '18 at 10:09
  • _"This method is **asynchronous**, meaning it may **not finish** before the **next** line in your sketch is executed..."_ ([Reference](http://p5js.org/reference/#/p5/loadJSON)) - You need to read the docs and use a callback function as explained in the duplicate – Alon Eitan Dec 02 '18 at 10:15
  • Ds = loadJSON('test' + 1 + '.json'); delay().then(function(tt){ console.log(Ds); console.log(date); }) Dosn't work in a loop?! – MrMugame Dec 02 '18 at 12:21

1 Answers1

0
function test(mynewdata) {
    console.log(mynewdata);
}

data = loadJSON('test' + i + '.json',test);

This works for me. Thanks!

MrMugame
  • 15
  • 4