Await Back-to-Back Node.js Events with events.once
A file importer emits opened when it has a job ID and ready when it has counted the rows. A fast path can emit both events in one call. A caller that awaits opened before it subscribes to ready can wait forever for an event the importer already sent.
Register both waits before starting the importer. The examples use the built-in node:events module and can run as .mjs files in Node.js 20 or later.
Reproduce the Missed Event
The abort controller lets this example finish after demonstrating the missed event.
| |
emit("ready", 3) returns false because no listener was registered for ready at that moment. The first emit() calls its listeners synchronously, but the code after await resumes in a promise microtask. The example awaits setImmediate() to give that continuation time to attach the ready listener before inspecting it. An EventEmitter delivers events to its current listeners and does not replay earlier events to late subscribers.
Node.js event timing documentation
,
Node.js timers documentation
Register Both Waits Before Starting Work
Save this helper as startup-events.mjs.
| |
Calling waitForStartup() installs both listeners before the function reaches its first await. Call it before invoking any producer method that might emit either event. Promise.all() combines the two results, while the calls to once() do the subscribing.
Each wait resolves to an array of arguments passed to its event. The nested destructuring reads the first argument of each array. The aggregate uses the order of the input promises, so jobId still comes from opened if ready arrives first.
Start the Producer After Subscribing
Save this as run-startup.mjs beside the helper and run node run-startup.mjs.
| |
Keep the call to startImport() between setting up the waits and awaiting their result. This example uses a fresh emitter for each import. If two jobs share an emitter and use the same event names, the helper can pick up a payload from the wrong job. Use one emitter per import when applying this pattern.
Cancel a Wait When Startup Ends Early
Pass the same AbortSignal to both waits if the caller might abandon startup. Aborting rejects any pending wait with an AbortError and removes its listeners. Aborting the wait leaves the producer running unless it also supports cancellation.
While a wait for opened or ready is pending, an error event on the same emitter rejects that wait. Once both waits settle, their temporary error listeners are gone. If the importer can emit errors later in its lifecycle, handle those errors at the importer level.
Node.js events.once API
Test Immediate Events, Errors, and Cancellation
Save this as test-startup-events.mjs and run node test-startup-events.mjs.
| |
The first loop checks that both listeners are present at emission time and that payloads map to the right fields in either order. The failure and cancellation cases check that startup rejects and leaves no temporary listeners behind, even when one event already arrived before cancellation.