so I have a function which open a txt file, extracts some lines and returns an array.
Problem is, it doesn't..
I console.log'd the last line before the return which printed an array, so I'm confused.
Tried console.logging everything but for some reason it will ALWAYS RETURN UNDEFINED
function Collect_Games(Amount){
let Msg = "",
userkeys = [];
fs.readFile("./SETTINGS/games.txt", "utf8", (error, data) => {
if(error) {
return;
} else {
let keys = data.split("\n");
if(keys.length < Amount) {
dont = false;
Msg = "I don't have enough keys for you, please try again later..";
} else {
userkeys = keys.splice(0, Amount);
/*
for(var i=0 ; i < userkeys.length ; i++){
Msg+= userkeys[i]+"\r\n";
}*/
fs.writeFile("./SETTINGS/games.txt", keys.join("\n"), error => {
if(error) {
}
console.log(userkeys); // this actually prints an array..
return userkeys;
});
}
}
});
}
Expecting this: console.log(Collect_Games(3));
to return an array, however it returns undefined