JavaScript Javascript / Nodejs use await on top level in nodejs module javascript node js await,js node js await,javascript async

I tried finding the solution to my problem, but couldnt find it, and was looking for some "best practice examples". I have a nodejs express application and my functions are split in files. For example I have this controller (oktacontroller.js):

var okta_api_key = <here some await data, getting from db>;

const OKTA_ORG_URL = '<here too>';

exports.createUser = async (req, res) => {

    console.log(okta_api_key);
}

exports.getGroups = async (req, res) => {
    console.log(okta_api_key);
}

In both exported functions (which are express routes) I need the var okta_api_key. I know I can get them by querying them in both functions, because they are async functions and I can use await there, but it feels dumb to query this every time (because it wont ever change).

How can I manage this? I know I can do this:

var okta_api_key;
(async () => {
    okta_api_key = await <getting key async>
})()

But this feels off as well..

Is there any way too make some sort of large function, which is async, and exports the two functions? In other words: How do I use await on the top level of a file (module). It doesnt need to be on top level (its impossible), but some sort of method to have my "top level" variables exposed to my exported functions.

EDIT: Some other usecase, because I got the suggestion of putting it in a config file. Yes, for this one it is possible, but for example: I have some other api key which gets his access token from the service itself, on every startup (because it expires). That token cannot be stored in the config file, so I need some async work to get the value. I know top-level-await is not working (or even not desirable), but I just want an example of how you guys would do this if it were your project :)

Answer:1

I often use this strategy to my java code in order to make a Collection read only for the outside world, but avoid big/often clonings: public abstract class MyClass { List<Long> myIds; ...

I often use this strategy to my java code in order to make a Collection read only for the outside world, but avoid big/often clonings: public abstract class MyClass { List<Long> myIds; ...

  1. equivalent javascript
  2. equivalent javascript pdf
  3. equivalent javascript book
  4. equivalent javadoc c#
  5. java equivalent of python dictionary
  6. java equivalent of linq
  7. javadoc equivalent for python
  8. javascript equivalent of document ready
  9. javascript equivalent of jquery find
  10. java equivalent of pass
  11. javascript equivalent of python range
  12. java equivalent of var
  13. java equivalent of struct
  14. javascript equivalent of linq select
  15. java equivalent of using
  16. javascript equivalent of pass
  17. java equivalent of dictionary
  18. java equivalent of pandas
  19. javascript equivalent of python dictionary
  20. java equivalent of c# dictionary

I'm trying to pass a string parameter using the link. but it seems that computed or methods property cannot return the parameter value. The whole component stops rendering when I use computed property....

I'm trying to pass a string parameter using the link. but it seems that computed or methods property cannot return the parameter value. The whole component stops rendering when I use computed property....

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. cancun
  6. canada goose
  7. cane corso
  8. canes
  9. canadian prime minister
  10. cancun flights
  11. canker sore
  12. candy
  13. cancer
  14. canon
  15. canvas prints
  16. cancer sign
  17. candace owens
  18. canada news
  19. candytopia
  20. canlis

I've got an infinite loop inside my while loop and I can't find the cause. It's a simple function that returns the sum of the argument's digits. I use a while loop because it needs to add up the ...

I've got an infinite loop inside my while loop and I can't find the cause. It's a simple function that returns the sum of the argument's digits. I use a while loop because it needs to add up the ...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. canada goose
  6. cancun flights
  7. cane corso
  8. canes
  9. canadian prime minister
  10. canker sore
  11. candy
  12. cancer
  13. cancun
  14. canon
  15. canvas prints
  16. cancer sign
  17. candace owens
  18. canada news
  19. candytopia
  20. canlis

I am very new to react world. I was gone through many examples of react, flux, webpack etc. I found that when I create a page as a react component it is bundled by webpack and loaded in the page. How ...

I am very new to react world. I was gone through many examples of react, flux, webpack etc. I found that when I create a page as a react component it is bundled by webpack and loaded in the page. How ...

  1. dynamically loading react components