I have a snippet of code that calls a separate util.js file to grab a given text file and read it and create an array from its contents:
function getDataFromTxt(fullFilePath) {
//gets a list from a txt file and returns 1 array
const myRequest = new Request(fullFilePath);
var toReturn;
fetch(myRequest)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error, status = ${response.status}`);
}
return response.text();
})
.then((text) => {
toReturn = text;
toReturn = toReturn.split("r");
for (let i = 0; i < toReturn.length; i++){
toReturn[i] = toReturn[i].replace('n','');
}
console.log(toReturn);
return toReturn;
})
.catch((error) => {
toReturn = `Error: ${error.message}`;
});
}
However, when its called, the order of operations of what is going on really has me confused…
console.log("before");
this.patrolArray = getDataFromTxt(textFile);
console.log("after");
This results in the console spitting out:
before
after
<array>
Am I having a brain fart here or is something going on that I’m not expecting. I am relatively new to JS so apologies if this is a dumb question.