To add asynchronous Node.js module exports, we use promises.
For instance, we write
module.exports = (async () => {
const db = await require("./db");
const foo = "bar";
return {
foo,
};
})();
in foo.js to export the promise returned by the async function.
Then we import it by writing
(async () => {
const foo = await require("./foo");
console.log(foo);
})();
to call require to require foo.js, which exported the promise returned by the async function.
We use await to get the object returned by the promise.