Gathering detailed insights and metrics for emittery
Gathering detailed insights and metrics for emittery
Gathering detailed insights and metrics for emittery
Gathering detailed insights and metrics for emittery
npm install emittery
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.7
Supply Chain
99.5
Quality
82.8
Maintenance
100
Vulnerability
100
License
JavaScript (87.28%)
TypeScript (12.72%)
Total Downloads
4,201,528,353
Last Day
1,683,923
Last Week
30,180,235
Last Month
130,380,792
Last Year
1,330,914,533
MIT License
1,952 Stars
113 Commits
76 Forks
15 Watchers
1 Branches
29 Contributors
Updated on Jun 29, 2025
Minified
Minified + Gzipped
Latest Version
1.2.0
Package Id
emittery@1.2.0
Unpacked Size
47.49 kB
Size
11.04 kB
File Count
6
NPM Version
10.9.2
Node Version
20.19.1
Published on
Jun 20, 2025
Cumulative downloads
Total Downloads
Last Day
-12.9%
1,683,923
Compared to previous day
Last Week
-9.2%
30,180,235
Compared to previous week
Last Month
5%
130,380,792
Compared to previous month
Last Year
13.9%
1,330,914,533
Compared to previous year
Simple and modern async event emitter
It works in Node.js and the browser (using a bundler).
Emitting events asynchronously is important for production code where you want the least amount of synchronous operations. Since JavaScript is single-threaded, no other code can run while doing synchronous operations. For Node.js, that means it will block other requests, defeating the strength of the platform, which is scalability through async. In the browser, a synchronous operation could potentially cause lags and block user interaction.
1npm install emittery
1import Emittery from 'emittery'; 2 3const emitter = new Emittery(); 4 5emitter.on('🦄', data => { 6 console.log(data); 7}); 8 9const myUnicorn = Symbol('🦄'); 10 11emitter.on(myUnicorn, data => { 12 console.log(`Unicorns love ${data}`); 13}); 14 15emitter.emit('🦄', '🌈'); // Will trigger printing '🌈' 16emitter.emit(myUnicorn, '🦋'); // Will trigger printing 'Unicorns love 🦋'
Emittery accepts strings, symbols, and numbers as event names.
Symbol event names are preferred given that they can be used to avoid name collisions when your classes are extended, especially for internal events.
Toggle debug mode for all instances.
Default: true
if the DEBUG
environment variable is set to emittery
or *
, otherwise false
.
Example:
1import Emittery from 'emittery'; 2 3Emittery.isDebugEnabled = true; 4 5const emitter1 = new Emittery({debug: {name: 'myEmitter1'}}); 6const emitter2 = new Emittery({debug: {name: 'myEmitter2'}}); 7 8emitter1.on('test', data => { 9 // … 10}); 11 12emitter2.on('otherTest', data => { 13 // … 14}); 15 16emitter1.emit('test'); 17//=> [16:43:20.417][emittery:subscribe][myEmitter1] Event Name: test 18// data: undefined 19 20emitter2.emit('otherTest'); 21//=> [16:43:20.417][emittery:subscribe][myEmitter2] Event Name: otherTest 22// data: undefined
Create a new instance of Emittery.
Type: object
Configure the new instance of Emittery.
Type: object
Configure the debugging options for this instance.
Type: string
Default: undefined
Define a name for the instance of Emittery to use when outputting debug data.
Example:
1import Emittery from 'emittery'; 2 3Emittery.isDebugEnabled = true; 4 5const emitter = new Emittery({debug: {name: 'myEmitter'}}); 6 7emitter.on('test', data => { 8 // … 9}); 10 11emitter.emit('test'); 12//=> [16:43:20.417][emittery:subscribe][myEmitter] Event Name: test 13// data: undefined
Type: boolean
Default: false
Toggle debug logging just for this instance.
Example:
1import Emittery from 'emittery'; 2 3const emitter1 = new Emittery({debug: {name: 'emitter1', enabled: true}}); 4const emitter2 = new Emittery({debug: {name: 'emitter2'}}); 5 6emitter1.on('test', data => { 7 // … 8}); 9 10emitter2.on('test', data => { 11 // … 12}); 13 14emitter1.emit('test'); 15//=> [16:43:20.417][emittery:subscribe][emitter1] Event Name: test 16// data: undefined 17 18emitter2.emit('test');
Type: Function(string, string, EventName?, Record<string, any>?) => void
Default:
1(type, debugName, eventName, eventData) => { 2 if (typeof eventData === 'object') { 3 eventData = JSON.stringify(eventData); 4 } 5 6 if (typeof eventName === 'symbol' || typeof eventName === 'number') { 7 eventName = eventName.toString(); 8 } 9 10 const currentTime = new Date(); 11 const logTime = `${currentTime.getHours()}:${currentTime.getMinutes()}:${currentTime.getSeconds()}.${currentTime.getMilliseconds()}`; 12 console.log(`[${logTime}][emittery:${type}][${debugName}] Event Name: ${eventName}\n\tdata: ${eventData}`); 13}
Function that handles debug data.
Example:
1import Emittery from 'emittery'; 2 3const myLogger = (type, debugName, eventName, eventData) => { 4 console.log(`[${type}]: ${eventName}`); 5}; 6 7const emitter = new Emittery({ 8 debug: { 9 name: 'myEmitter', 10 enabled: true, 11 logger: myLogger 12 } 13}); 14 15emitter.on('test', data => { 16 // … 17}); 18 19emitter.emit('test'); 20//=> [subscribe]: test
Subscribe to one or more events.
Returns an unsubscribe method.
Using the same listener multiple times for the same event will result in only one method call per emitted event.
1import Emittery from 'emittery'; 2 3const emitter = new Emittery(); 4 5emitter.on('🦄', data => { 6 console.log(data); 7}); 8 9emitter.on(['🦄', '🐶'], data => { 10 console.log(data); 11}); 12 13emitter.emit('🦄', '🌈'); // log => '🌈' x2 14emitter.emit('🐶', '🍖'); // log => '🍖'
You can pass an abort signal to unsubscribe too:
1import Emittery from 'emittery'; 2 3const abortController = new AbortController(); 4 5emitter.on('🐗', data => { 6 console.log(data); 7}, {signal: abortController.signal}); 8 9abortController.abort(); 10emitter.emit('🐗', '🍞'); // nothing happens
Emittery exports some symbols which represent "meta" events that can be passed to Emitter.on
and similar methods.
Emittery.listenerAdded
- Fires when an event listener was added.Emittery.listenerRemoved
- Fires when an event listener was removed.1import Emittery from 'emittery'; 2 3const emitter = new Emittery(); 4 5emitter.on(Emittery.listenerAdded, ({listener, eventName}) => { 6 console.log(listener); 7 //=> data => {} 8 9 console.log(eventName); 10 //=> '🦄' 11}); 12 13emitter.on('🦄', data => { 14 // Handle data 15});
listener
- The listener that was added.eventName
- The name of the event that was added or removed if .on()
or .off()
was used, or undefined
if .onAny()
or .offAny()
was used.Only events that are not of this type are able to trigger these events.
Remove one or more event subscriptions.
1import Emittery from 'emittery'; 2 3const emitter = new Emittery(); 4 5const listener = data => { 6 console.log(data); 7}; 8 9emitter.on(['🦄', '🐶', '🦊'], listener); 10await emitter.emit('🦄', 'a'); 11await emitter.emit('🐶', 'b'); 12await emitter.emit('🦊', 'c'); 13emitter.off('🦄', listener); 14emitter.off(['🐶', '🦊'], listener); 15await emitter.emit('🦄', 'a'); // Nothing happens 16await emitter.emit('🐶', 'b'); // Nothing happens 17await emitter.emit('🦊', 'c'); // Nothing happens
Subscribe to one or more events only once. It will be unsubscribed after the first event that matches the predicate (if provided).
Returns a promise for the event data when eventName
is emitted and predicate matches (if provided). This promise is extended with an off
method.
1import Emittery from 'emittery'; 2 3const emitter = new Emittery(); 4 5emitter.once('🦄').then(data => { 6 console.log(data); 7 //=> '🌈' 8}); 9 10emitter.once(['🦄', '🐶']).then(data => { 11 console.log(data); 12}); 13 14// With predicate 15emitter.once('data', data => data.ok === true).then(data => { 16 console.log(data); 17 //=> {ok: true, value: 42} 18}); 19 20emitter.emit('🦄', '🌈'); // Log => '🌈' x2 21emitter.emit('🐶', '🍖'); // Nothing happens 22emitter.emit('data', {ok: false}); // Nothing happens 23emitter.emit('data', {ok: true, value: 42}); // Log => {ok: true, value: 42}
Get an async iterator which buffers data each time an event is emitted.
Call return()
on the iterator to remove the subscription.
1import Emittery from 'emittery'; 2 3const emitter = new Emittery(); 4const iterator = emitter.events('🦄'); 5 6emitter.emit('🦄', '🌈1'); // Buffered 7emitter.emit('🦄', '🌈2'); // Buffered 8 9iterator 10 .next() 11 .then(({value, done}) => { 12 // done === false 13 // value === '🌈1' 14 return iterator.next(); 15 }) 16 .then(({value, done}) => { 17 // done === false 18 // value === '🌈2' 19 // Revoke subscription 20 return iterator.return(); 21 }) 22 .then(({done}) => { 23 // done === true 24 });
In practice, you would usually consume the events using the for await statement. In that case, to revoke the subscription simply break the loop.
1import Emittery from 'emittery'; 2 3const emitter = new Emittery(); 4const iterator = emitter.events('🦄'); 5 6emitter.emit('🦄', '🌈1'); // Buffered 7emitter.emit('🦄', '🌈2'); // Buffered 8 9// In an async context. 10for await (const data of iterator) { 11 if (data === '🌈2') { 12 break; // Revoke the subscription when we see the value '🌈2'. 13 } 14}
It accepts multiple event names.
1import Emittery from 'emittery'; 2 3const emitter = new Emittery(); 4const iterator = emitter.events(['🦄', '🦊']); 5 6emitter.emit('🦄', '🌈1'); // Buffered 7emitter.emit('🦊', '🌈2'); // Buffered 8 9iterator 10 .next() 11 .then(({value, done}) => { 12 // done === false 13 // value === '🌈1' 14 return iterator.next(); 15 }) 16 .then(({value, done}) => { 17 // done === false 18 // value === '🌈2' 19 // Revoke subscription 20 return iterator.return(); 21 }) 22 .then(({done}) => { 23 // done === true 24 });
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
Returns a promise that resolves when all the event listeners are done. Done meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
Same as above, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer emit()
whenever possible.
If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will not be called.
Subscribe to be notified about any event.
Returns a method to unsubscribe. Abort signal is respected too.
Remove an onAny
subscription.
Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
Call return()
on the iterator to remove the subscription.
1import Emittery from 'emittery'; 2 3const emitter = new Emittery(); 4const iterator = emitter.anyEvent(); 5 6emitter.emit('🦄', '🌈1'); // Buffered 7emitter.emit('🌟', '🌈2'); // Buffered 8 9iterator.next() 10 .then(({value, done}) => { 11 // done === false 12 // value is ['🦄', '🌈1'] 13 return iterator.next(); 14 }) 15 .then(({value, done}) => { 16 // done === false 17 // value is ['🌟', '🌈2'] 18 // Revoke subscription 19 return iterator.return(); 20 }) 21 .then(({done}) => { 22 // done === true 23 });
In the same way as for events
, you can subscribe by using the for await
statement
Clear all event listeners on the instance.
If eventNames
is given, only the listeners for that events are cleared.
The number of listeners for the eventNames
or all events if not specified.
Bind the given methodNames
, or all Emittery
methods if methodNames
is not defined, into the target
object.
1import Emittery from 'emittery'; 2 3const object = {}; 4 5new Emittery().bindMethods(object); 6 7object.emit('event');
The default Emittery
class has generic types that can be provided by TypeScript users to strongly type the list of events and the data passed to their event listeners.
1import Emittery from 'emittery'; 2 3const emitter = new Emittery< 4 // Pass `{[eventName]: undefined | <eventArg>}` as the first type argument for events that pass data to their listeners. 5 // A value of `undefined` in this map means the event listeners should expect no data, and a type other than `undefined` means the listeners will receive one argument of that type. 6 { 7 open: string, 8 close: undefined 9 } 10>(); 11 12// Typechecks just fine because the data type for the `open` event is `string`. 13emitter.emit('open', 'foo\n'); 14 15// Typechecks just fine because `close` is present but points to undefined in the event data type map. 16emitter.emit('close'); 17 18// TS compilation error because `1` isn't assignable to `string`. 19emitter.emit('open', 1); 20 21// TS compilation error because `other` isn't defined in the event data type map. 22emitter.emit('other');
A decorator which mixins Emittery
as property emitteryPropertyName
and methodNames
, or all Emittery
methods if methodNames
is not defined, into the target class.
1import Emittery from 'emittery'; 2 3@Emittery.mixin('emittery') 4class MyClass {} 5 6const instance = new MyClass(); 7 8instance.emit('event');
Listeners are not invoked for events emitted before the listener was added. Removing a listener will prevent that listener from being invoked, even if events are in the process of being (asynchronously!) emitted. This also applies to .clearListeners()
, which removes all listeners. Listeners will be called in the order they were added. So-called any listeners are called after event-specific listeners.
Note that when using .emitSerial()
, a slow listener will delay invocation of subsequent listeners. It's possible for newer events to overtake older ones.
Emittery can collect and log debug information.
To enable this feature set the DEBUG environment variable to 'emittery'
or '*'
. Additionally you can set the static isDebugEnabled
variable to true on the Emittery class, or myEmitter.debug.enabled
on an instance of it for debugging a single instance.
See API for more details on how debugging works.
EventEmitter
in Node.js?There are many things to not like about EventEmitter
: its huge API surface, synchronous event emitting, magic error event, flawed memory leak detection. Emittery has none of that.
EventEmitter
synchronous for a reason?Mostly backwards compatibility reasons. The Node.js team can't break the whole ecosystem.
It also allows silly code like this:
1let unicorn = false; 2 3emitter.on('🦄', () => { 4 unicorn = true; 5}); 6 7emitter.emit('🦄'); 8 9console.log(unicorn); 10//=> true
But I would argue doing that shows a deeper lack of Node.js and async comprehension and is not something we should optimize for. The benefit of async emitting is much greater.
emit()
?No, just use destructuring:
1emitter.on('🦄', ([foo, bar]) => { 2 console.log(foo, bar); 3}); 4 5emitter.emit('🦄', [foo, bar]);
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More