Putting async at the Top Level of Node
The use of async/await in Javascript is a nice way to make traditional Promise-based code more linear, and yet for the top-level code in a Node script, await can't easily be used, because it's not within an async function. Looking at the traditional top-level script for a Node/Express project, you would look at bin/www and see:
#!/usr/bin/env node // dotenv is only installed in local dev; in prod environment variables will be // injected through Google Secrets Manager try { const dotenv = require('dotenv') dotenv.config() } catch { // Swallow expected error in prod. } // load up all the dependencies we need const app = require('../app') const debug = require('debug')('api:server') const http = require('http')
which starts off by loading the dotenv function to read the environment variables into the Node process, and then start loading up the application. But you can't just toss in an await if you need to make some network calls... or a database call.
Sure, you can use a .then() and .catch(), and put the rest of the startup script into the body of the .then()... but that's a little harder to reason through, and if you need another Promise call, it only nests, or another .then().
Possible, but not clean.
If we wrap the entire script in an async function, like:
#!/usr/bin/env node (async () => { // normal startup code })();
then the bulk of the bin/www script is now within an async function, and so we can use await without any problems:
#!/usr/bin/env node (async () => { // dotenv is only installed in local dev; in prod environment variables will be // injected through Google Secrets Manager try { const dotenv = require('dotenv') dotenv.config() } catch { // Swallow expected error in prod. } // augment the environment from the Cloud Secrets try { const { addSecretsToEnv } = require('../secrets') await addSecretsToEnv() } catch (err) { console.error(err) } // load up all the dependencies we need const app = require('../app') const debug = require('debug')('api:server') const http = require('http')
While this indents the bulk of the bin/www script, which stylistically, isn't as clean as no-indent, it allows the remainder of the script to use await without any problem.
Not a bad solution to the problem.