re: How can I read a local JSON file in HTML/JavaScript? VIEW POST


Ok, thank you, is there a way to transport data outside of the walls of the scope of it's promise?

let y = null;

Promise {
    // [Promise value scope]
    let x = data;
    y = data; // ? Is this possible, is this allowed?


You could assign it to a variable outside the scope of the promise. However, you can't be sure if the value is available in the variable when you access it because promise resolution is asynchronous. Hence the code outside the promise which falls after the promise call might run before the promise even resolves and gives you the JSON data.

Could I, theoretically, have a while loop running until the variable is not null? Or is it just a matter of either it works or it doesn't?
Thanks for your help thus far, I really appreciate it!

Of course, you can. But I wouldn't recommend it. It is only good enough to be known that it is possible that way but not enough to use it in practice. You can perhaps leverage async/await to write a somewhat synchronous looking code.

code of conduct - report abuse