DEV Community

Discussion on: Adventures of a Hobbyist ~ Part Three

Collapse
 
avalander profile image
Avalander • Edited

You could use dotenv to load configuration variables easily from a .env file. It's very convenient for database urls and api keys and similar things.

If you want to keep your solution to read the config file, though, there are a couple of things you can do about the pending promise:

  1. The easiest is to load the file with fs.readFileSync. If you are loading the file only once at start up, there is no reason why you need that call to be async.

  2. If you really need a start up operation that is asynchronous (like connecting to a MongoDB instance), you can hook your start up function at the point where the promise resolves and inject the resolved value to all the code that is dependant on it.

const startApp = conf => {
    // Do everything that you need to start your app here.
}

const conf = await ch.loadConf()
startApp(conf)

I don't use async/await, but you can take a look at how I start an express server after initiating a connection to MongoDB here.

To reiterate though, I strongly suggest you use dotenv to store database urls and similar data that you don't want to commit to your version control system. And if that doesn't suit you for any reason, there is likely no good reason not to use fs.readFileSync to read your config file at the app start.

Collapse
 
link2twenty profile image
Andrew Bone

dotenv looks like it does exactly what I need, thank you so much 🙂