0.2.1 • Published 6 years ago

@cfware/promisify-emitter v0.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

@cfware/promisify-emitter

Travis CI Coverage Status Greenkeeper badge NPM Version NPM Downloads MIT

Initiate a promise that resolves or rejects based on emitter events.

Install @cfware/promisify-emitter

This module requires node.js 6 or above.

npm i --save @cfware/promisify-emitter

Usage

Direct usage on emitter that is not yet resolved

const koa = require('koa');
const promisifyEmitter = require('@cfware/promisify-emitter');

async function main() {
	const app = new koa();

	app.use(ctx => ctx.body = 'Hello world!');

	const server = await promisifyEmitter(app.listen(0), 'listening');

	console.log(`Listening at http://localhost:${server.address().port}/`);
}

main().catch(console.error);

Extending class with a replacement function

const koa_base = require('koa');
const promisifyEmitter = require('@cfware/promisify-emitter');

class koa extends koa_base {
	listen(...args) {
		return promisifyEmitter(super.listen(...args), 'listening');
	}
}

async function main() {
	const app = new koa();

	app.use(ctx => ctx.body = 'Hello world!');

	const server = await app.listen(0);

	console.log(`Listening at http://localhost:${server.address().port}/`);
}

main().catch(console.error);

Running tests

Tests are provided by eslint and mocha.

npm install
npm test