With PapaParse I'm parsing a csv file which I then add to variable dataShort and dataTotal. I am able to log both dataShort and dataTotal inside of the complete function but not below it. I suppose this happens because the function runs asynchronously.
As a result of that the following functions (both changeData and dataReady) can't run successfully since they require Papa Parse to be done with parsing. How can I make changeData(); in the function parseCSV run only when the parsing above is complete?
function parseCSV(evt) {
var file = evt.target.files[0];
Papa.parse(file, {
header: true,
dynamicTyping: true,
complete: function (results) {
dataShort = results.data;
dataTotal = results.data;
console.log(results.data);
console.log("dataShort: " + dataShort);
console.log("dataTotal: " + dataTotal);
}
});
console.log("dataShort: " + dataShort);
console.log("dataTotal: " + dataTotal);
changeData();
}
function changeData() {
$.each(dataShort, function (index, item) {
if (item["Items in CHILDoc"].length > 20) {
item["Items in CHILDoc"] = item["Items in CHILDoc"].substring(0, 18) + "...";
}
if (item["Kwadrant onderdeel in CHILDoc"] != "Ingrijpende gebeurtenissen" && item["Waarde in CHILDoc"].length > 20) {
item["Waarde in CHILDoc"] = item["Waarde in CHILDoc"].substring(0, 15) + "...";
}
})
// console.log(dataShort);
// console.log(dataTotal);
dataReady();
}