Skip to content

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

License

Notifications You must be signed in to change notification settings

AlvaroBernalG/ts-promisify-event-emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-promisify-event-emitter

Another implementation of Event Emitter but with Promises

travis npm version

Install

$ npm install ts-promisify-event-emitter

Usage

Basic usage:

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<IUser> {
  // do something async
  return {
    name: 'Test',
    lastname: 'Testing'
  }
}

const events = new EventEmitterPromisified<IQuery, IUser>();

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

events.on('getUser', callback);

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

Deno

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published