{"id":13159,"library":"event-emitter-promisify","title":"Promisify Event Emitters","description":"event-emitter-promisify is a lightweight JavaScript and TypeScript utility library designed to convert Node.js `EventEmitter` instances into standard Promises. It enables developers to integrate asynchronous `async/await` syntax with traditional event-driven patterns, specifically by resolving a Promise when a success event (defaulting to 'end') is emitted, and rejecting it upon an 'error' event. The library provides a single, focused function, `promisifyEventEmitter`, which simplifies handling the lifecycle of event emitters in a Promise-based workflow. The current stable version is 1.1.0, released in February 2022. While functional and stable, the project exhibits an infrequent release cadence, with no major updates since its initial introduction. Its primary differentiation lies in offering a specialized solution for EventEmitters, distinguishing it from general-purpose promisification utilities like `util.promisify` that target Node.js-style callback functions.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/jeswr/promisify-event-emitter","tags":["javascript","promise","eventemitter","promisify","event","emitter","async","typescript"],"install":[{"cmd":"npm install event-emitter-promisify","lang":"bash","label":"npm"},{"cmd":"yarn add event-emitter-promisify","lang":"bash","label":"yarn"},{"cmd":"pnpm add event-emitter-promisify","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This library is primarily designed for ESM environments, although CommonJS `require` might work with transpilation. TypeScript types are included.","wrong":"const promisifyEventEmitter = require('event-emitter-promisify')","symbol":"promisifyEventEmitter","correct":"import { promisifyEventEmitter } from 'event-emitter-promisify'"},{"note":"When importing only the type, use `import type` for better tree-shaking and explicit intent.","wrong":"import { PromisifyEventEmitter } from 'event-emitter-promisify'","symbol":"promisifyEventEmitter (TypeScript Type)","correct":"import type { PromisifyEventEmitter } from 'event-emitter-promisify'"}],"quickstart":{"code":"import { promisifyEventEmitter } from 'event-emitter-promisify';\nimport { Readable } from 'stream';\n\nasync function processStream() {\n  const stream = new Readable({\n    read() {\n      this.push('some data');\n      this.push(null); // Signal end of stream\n    }\n  });\n\n  let receivedData = '';\n  const promise = promisifyEventEmitter(\n    stream.on('data', (chunk) => {\n      receivedData += chunk.toString();\n    })\n  );\n\n  try {\n    await promise;\n    console.log('Stream processed successfully. Received:', receivedData);\n  } catch (error) {\n    console.error('Stream encountered an error:', error.message);\n  }\n}\n\nprocessStream();","lang":"typescript","description":"Demonstrates how to use `promisifyEventEmitter` with a simple Node.js Readable stream, collecting data and awaiting its completion."},"warnings":[{"fix":"Update `package.json` to depend on `event-emitter-promisify` and change all import statements from `@jeswr/promisify-event-emitter` to `event-emitter-promisify`.","message":"The package was renamed from `@jeswr/promisify-event-emitter` to `event-emitter-promisify` in version 1.1.0. Projects using the older scoped name must update their `package.json` and import paths.","severity":"breaking","affected_versions":">=1.1.0"},{"fix":"Ensure your `EventEmitter` pipeline reliably emits an `end` event upon completion, or provide a second argument to `promisifyEventEmitter` to specify a custom event that signifies success and a return value.","message":"By default, the promise resolves when the `end` event is emitted. If your `EventEmitter` never emits an `end` event, the promise will never resolve unless an `error` event is emitted or a custom success event is configured.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Implement robust error handling within your `EventEmitter` producer to ensure that any failure condition results in an `error` event being emitted. Alternatively, introduce a timeout mechanism for the promise if hanging is a concern.","message":"The promise will only reject if the `error` event is emitted by the `EventEmitter`. If an underlying asynchronous operation fails without emitting an `error` event, the promise may hang indefinitely.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure the variable passed to `promisifyEventEmitter` is an instance of `EventEmitter` or an object that implements the `on` method (e.g., a `Readable` stream, `net.Socket`, etc.).","cause":"You are attempting to use `promisifyEventEmitter` with an object that does not expose an `.on()` method, meaning it's not a valid `EventEmitter` instance.","error":"TypeError: stream.on is not a function"},{"fix":"Review the logic of your `EventEmitter` producer to verify that it eventually emits a 'end' event (or your specified custom success event) when its operation completes, or an 'error' event if a failure occurs. Debug the EventEmitter's event flow.","cause":"The `EventEmitter` passed to `promisifyEventEmitter` (or its underlying mechanism) is not emitting the 'end' event (or a custom configured success event) and is also not emitting an 'error' event.","error":"Promise never resolves (application hangs)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}