New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

event-promisify

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-promisify

Promisification for EventEmitters.

  • 0.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 29 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc