Giter Club home page Giter Club logo

event-promisify's Introduction

event-promisify

NPM

Build Status Dependencies npm version

Just run npm install event-promisify

Documentation

This implementation supports basic promisification, event filtering, aggregation, custom specification of error events and is promise library agnostic (defaults to native/global Promise object).

Errors

Keep in mind that some promise implementations do not take kindly to using non-errors for rejections. Therefor when a(an) (custom) error event, the event value will be contained in an error object, but only if the event is not an error already. If the emitted object is a string however, the string will be used for the errors message, no event property will be added!

Multiple emitted values

Not supported, multiple emitted values is usually a sign of bad design. If you happen to find a good reason why this should be supported, please create an issue. Meanwhile this will suffice.

function fixMultiArgEventEmitter(emitter, name, newName) {
	emitter.on(name, function() { emitter.emit(newName, Array.prototype.slice.call(arguments)));
}

Examples

Simple promisification

const promisifyEvent = require('event-promisify')(YOUR_PROMISE_CONSTRUCTOR_HERE);

promisifyEvent(someEventEmitter, 'someEvent')
.then(...)

Event filtering

promisifyEvent(someEventEmitter, {
	name: 'someEventThatOccursmultipleTimes',
	filter: event => someConditionThatReturnsABooleanWhenYouAreDone(event)
})
.then(...)

Event aggregation

promisifyEvent(someEventEmitter, {
	name: 'end',
	aggregate: 'data'
})
.then(result => console.log( /* array of aggregated event data */ result.aggregated, /* final event data */ result.value))

Custom termination event

promisifyEvent(someEventEmitter, {
	name: 'end',
	errorName: 'disconnect'
})
.catch(error => ...)

Ignore errors

Custom errors will still be handled.

It usually means you did something wrong if this happens, but some libraries may emit intermediate errors when they actually should emit warnings or similar.

Be also warned that this MAY lead to promises not settling!

promisifyEvent(someEventEmitter, {
	name: 'end',
	ignoreErrors: true
})
.then(...)

For now more info check test.js and the index.js for further information.

event-promisify's People

Contributors

simonschick avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.