Skip to content

Instantly share code, notes, and snippets.

@davistrent
Created August 6, 2017 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davistrent/5ebcbf2742e211b30df0213ad55f3ac0 to your computer and use it in GitHub Desktop.
Save davistrent/5ebcbf2742e211b30df0213ad55f3ac0 to your computer and use it in GitHub Desktop.
async config
module.exports = new Promise((resolve, reject) => {
console.log('config module loaded!');
const foo = {};
getAsyncConfig(foo)
.then(config => {
resolve(config);
});
});
function getAsyncConfig(foo) {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
foo.bar = function(test) {
console.log('foo.bar() executed with ' + test);
};
foo.env = 'development';
foo.port = 8080;
resolve(foo);
}, 2000);
});
return promise;
}
require('./config')
.then(config => {
console.log(config);
require('./server')(config);
});
const express = require('express');
function server(settings) {
const config = {};
Object.assign(config, settings);
const PORT = config.port;
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(PORT, () => {
console.log(`server listening on port ${PORT}`);
});
return app;
};
module.exports = server;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment