I'm new to JS/nodejs, so please pardon me if I can't ask to-the-point question.
So basically, if I have two async functions,
async function init() {...}
async function main() {...}
How can I make sure to call main() after init() has finished its async requests?
Specifically, I want to make use of the module https://www.npmjs.com/package/hot-import
whereas on its page, there is a sample code:
async function main() {
const MODULE_CODE_42 = 'module.exports = () => 42'
const MODULE_CODE_17 = 'module.exports.default = () => 17'
const MODULE_FILE = path.join(__dirname, 't.js')
fs.writeFileSync(MODULE_FILE, MODULE_CODE_42)
const hotMod = await hotImport(MODULE_FILE)
. . .
The sample code works as it is, but when I put that into a event call back function, things start to break -- It works for the first event trigger but not the second.
I think the problem is not the constant hotMod, but the await hotImport in async function that is causing the problem. Thus I'm trying to define hotMod as a global variable and do hotMod = await hotImport(MODULE_FILE) in a async init() function before main() is called. But so far I've not been able to, as I'm quite new to JS/nodejs.
Please help. Thx.
mainmultiple times anyway, right? Please tell us more about what exactly breaks apart when you put something into an event handler, and post a minimal reproducible example with the not working code.hotImport()because if it isn't returning a promise that is linked to when it's done, then theawaitwon't do what you want.