From c1956ac62373e860a62b025d7f94c9e4585139d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Sun, 6 May 2018 12:57:44 +0200 Subject: [PATCH] Clean up using servers listening and error events --- lib/server-impl.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/server-impl.js b/lib/server-impl.js index 9b1a1d80ca..bb15b81d2b 100644 --- a/lib/server-impl.js +++ b/lib/server-impl.js @@ -28,14 +28,13 @@ function createApp(options) { const app = getApp(config); startMonitoring(options.serverMetrics, eventBus); - return new Promise(resolve => { - const server = app.listen( - { port: options.port, host: options.host }, - () => { - logger.info(`Unleash started on port ${server.address().port}`); - resolve({ app, server, eventBus }); - } - ); + const server = app.listen({ port: options.port, host: options.host }, () => + logger.info(`Unleash started on port ${server.address().port}`) + ); + + return new Promise((resolve, reject) => { + server.on('listening', () => resolve({ app, server, eventBus })); + server.on('error', reject); }); } @@ -44,8 +43,7 @@ function start(opts) { return migrator({ databaseUrl: options.databaseUrl }) .catch(err => logger.error('failed to migrate db', err)) - .then(() => createApp(options)) - .catch(err => logger.error('failed creating app', err)); + .then(() => createApp(options)); } module.exports = {