4

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.

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. Commented Dec 22, 2018 at 20:10
  • @Bergi, true, I totally agree. However, the program that I'm writing is for github.com/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. 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 the await won't do what you want. Commented Dec 22, 2018 at 23:55

3 Answers 3

7

using 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Varun for thinking logically and looking through what I'm trying to ask and give the to-the-point answer that hits the nail right on its head!!! BINGO! Splendid simple answer for a simple question like my OP. Thanks!
6

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()
})

2 Comments

Even simpler: init.then(main)
No doubt, @Bergi -- just trying to make the explicit connection in case there's a return value from init()
2

To run them synchronously, just do:

await init()
await main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.