This is probably dead simple, but I can't quite figure it out. I simply want to read the contexts of a text file into a variable. What I have is:
async function load(path) {
try {
const response = await fetch(path);
const text = await response.text();
return text;
} catch (err) {
console.error(err);
}
}
var source_text = load(source_text_path);
console.log(source_text);
To my mind, this should work but only the pending promise is returned and not the text, thought I thought it was awaiting properly.
loadbecause it is asynchronous (or chain it with athen)initfunctions which some (also async) main function will call (and await) on startup. The synchronous top-level code then doesmain().catch(e => { console.error(e); process.exit(1) })awaitin top-level code (because importing a module is in itself an asynchronous operation, unlikerequire).