The basic premise of this is that I am trying to have a function within the class Manager wait for a value to be present before it begins. I've tried every variation of the code below and cannot seem to make it work. It does log "Special value found" so I know that it's returning the value. Why does this not work? I clearly don't understand async well.
async function getValue (file) {
let specialI = false;
fs.readFile(file, 'utf8', (err, fileContents) => {
for (let i in data) {
if (data[i] === GOOD) {
specialI = true;
}
}
if (specialI) {
console.log("Special value found!");
return data[specialI];
} else {
return false;
}
}
}
class manager() {
async waitForSpecial() {
let value;
value = await getValue("file.json");
if (value) {
console.log("Wow it worked!");
} else {
console.log("Still no value...");
await sleep(500);
this.waitForSpecial();
}
}
}