UNPKG

omniwheel

Version:

Boilerplate reduction for backend and microservices. Scale your application easily in any direction.

38 lines (37 loc) 1.41 kB
/** * Invoke the object's callback-based listen() method, returning a Promise to the created server. * The Promise is resolved only as soon as the server is listening, as indicated by its 'listening' event. * * This function can be used to start Express applications, but also regular http.Server instances etc., * as long as they have a compatible listen() method. * * @param app The application to start. * @param port The port to listen on. * @param hostname The hostname to listen on. * @returns A Promise that resolves to the started server. */ export async function promisifiedListen(app, port, hostname) { return await new Promise((resolve, reject) => { const httpServer = hostname != null ? app.listen(port, hostname) : app.listen(port); httpServer.once('listening', () => resolve(httpServer)); httpServer.once('error', reject); }); } /** * Invoke the object's callback-based close() method, returning a Promise. * * This is intended to stop http.Server instances but can be used for anything with a compatible close() method. * * @param server The server to close. * @returns A Promise that resolves when done. */ export async function promisifiedClose(server) { await new Promise((resolve, reject) => server.close((err) => { if (err != null) { reject(err); } else { resolve(); } })); }