I want to parse the contents of a JSON file and store them in an object property data.current
, outside of the promise used to read the file:
HTML
<form name="data">
<label>
<span>Load Data</span>
<input type="file" accept="application/json">
</label>
</form>
JavaScript
const data = {
input: document.forms["data"].querySelector("input"),
current: {}
}
data.input.addEventListener("change", function(event) {
if(Object.entries(data.current).length == 0) {
event.target.files[0].text()
.then((content) => {
data.current = JSON.parse(content);
console.log(data.current)
});
console.log(data.current)
}
});
Outputs
Line 11: Object { property1: [...], property2: [...], property3: [...] }
Line 13: Object { }
Expected Outputs
Line 11: Object { property1: [...], property2: [...], property3: [...] }
Line 13: Object { property1: [...], property2: [...], property3: [...] }
The first console.log(data.current)
shows a parsed JSON Object, the second one an empty Object. How can I get the parsed JSON outside of the promise?