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
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.
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./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.
Share Improve this question asked Dec 22, 2018 at 20:00 xptxpt 23k45 gold badges150 silver badges244 bronze badges 3-
No, I don't think making a global promise variable will help anything. You're not calling
main
multiple 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. – Bergi Commented Dec 22, 2018 at 20:10 - @Bergi, true, I totally agree. However, the program that I'm writing is for github./Chatie/wechaty, I can give a minimal example for that, but it depends on a monster amount of modules, and I don't know nodejs good enough to give another event triggering example in a minimal way. I do feel that making a global promise variable will not help anything though. This is my dilemma. – xpt Commented Dec 22, 2018 at 20:22
-
You need to show us the relevant code for the circumstance that doesn't work, describe what you wanted to happen and describe what you observed that was different than that. Right now, it looks like you're just showing us a piece. And, you probably need to show the code for
hotImport()
because if it isn't returning a promise that is linked to when it's done, then theawait
won't do what you want. – jfriend00 Commented Dec 22, 2018 at 23:55
3 Answers
Reset to default 7using aysnc await
async function myFlow(){
.....
await init();
main();
....
}
In above code main() will be called only when init is resolved(). I hope this helps you.
async function return promises. So you should be able to call one after the other with then()
init()
.then(() => main())
If init
returns something (for example hotMod
), you can pick it up as a parameter to then's callback.
init()
.then((init_return) => {
// do something with init_return
return main()
})
To run them synchronously, just do:
await init()
await main()