Node.js Reference ยท Events and Timers
Node.js emitter.emit() method
Reference for emitter.emit() in Node.js: synchronous listener calls, arguments, the boolean return value, thrown errors and emitting while listeners change.
emitter.emit(eventName, ...args) calls every listener registered for eventName, in order, passing along the extra arguments. It returns true if there was at least one listener and false otherwise.
The call is synchronous. By the time emit() returns, all listeners have finished (or one has thrown). That makes it predictable, but it also means a slow listener blocks the code that emitted.
const heard = emitter.emit('saved', id, name);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
emitter.emit(eventName: string | symbol, ...args: any[]): boolean
| Name | Description |
|---|---|
eventName |
The event to trigger. |
args |
Zero or more values passed to each listener, as separate arguments. |
Returns: true if the event had listeners, false if not.
Errors: an exception thrown by a listener propagates out of emit() and later listeners do not run. Emitting 'error' with no 'error' listener throws (see The error event).
Usage notes
- Snapshot semantics.
emit()copies the listener list before it starts. Listeners added during the emit wait until the next one; listeners removed during the emit still run in this one. - Reentrancy. A listener may emit again, even the same event. That nested emit runs fully before the outer one resumes. This is a classic source of surprise ordering and stack overflows.
- Pass one object for many values. Positional arguments are fine for two or three values; beyond that a single options object keeps listeners readable, and all listeners share the same reference.
- No return values from listeners. Whatever a listener returns is discarded. If you need a result, have the listener mutate an argument or use a callback.
- Do not
await emit(). It returns a boolean, not a promise. Async listeners start running butemit()does not wait for them. If you need to wait, gather results yourself or useevents.once(). - Emitting in constructors. Listeners cannot be attached before
newhas returned, so an event emitted from a constructor is always lost. Defer it withprocess.nextTick()orqueueMicrotask(). - Return value use. The boolean is mostly useful for the
'error'event or for logging when a message had no audience. - Performance. Emitting is a plain loop over an array; one or a few listeners cost almost nothing, and the variable-argument call is a small overhead.