Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alvarobernalg/ts-promisify-event-emitter

Another implementation of Event Emitter but strongly typed, "promisified" and with configurable max listeners
https://github.com/alvarobernalg/ts-promisify-event-emitter

deno emitter event listener node promise

Last synced: about 1 month ago
JSON representation

Another implementation of Event Emitter but strongly typed, "promisified" and with configurable max listeners

Awesome Lists containing this project

README

        

# ts-promisify-event-emitter
> Another implementation of [Event Emitter](https://nodejs.org/api/events.html) but with Promises

[![travis](https://travis-ci.org/AlvaroBernalG/ts-promisify-event-emitter.svg?branch=master)](https://travis-ci.org/AlvaroBernalG/ts-promisify-event-emitter.svg?branch=master) [![npm version](https://badge.fury.io/js/ts-promisify-event-emitter.svg)](https://badge.fury.io/js/ts-promisify-event-emitter)

## Install
```
$ npm install ts-promisify-event-emitter
```

## Usage

Basic usage:

```ts
import EventEmitterPromisified, {Message, Callback} from 'ts-promisify-event-emitter';

// Result type
interface IUser {
name: string;
lastname: string;
}
// Query type
interface IQuery {
id: string;
}

async function getUserFromDatabase(id: string): Promise {
// do something async
return {
name: 'Test',
lastname: 'Testing'
}
}

const events = new EventEmitterPromisified();

const callback: Callback = async (message: Message): Promise => {
const user: IUser = await getUserFromDatabase(message.payload.id);
return user;
}

events.on('getUser', callback);

(async function start() {
const query: Message = {payload: {id: "ID-1234"}};
const [user] = await events.emit('getUser', query);
console.log(`The user is: Name=${user.name}, lastname=${user.lastname}.`);
})();
```

### Deno
```ts
import EventEmitterPromisified, {Message, Callback} from 'https://alvarobg.com/deno/ts_promisify_event_emitter/1.1.10/index.ts';

```
## Contributing

All contributions are welcome.

## License

MIT