A Node.js job can emit ready and done in a single call stack. If you await ready and only then subscribe to done, the second event might have happened before the await resumed. Register both one-shot listeners before invoking the producer, then give those listeners a shared cleanup path.
Prerequisites and fixture
Use Node.js 26 and save this as events.mjs. It uses one emitter for one active cycle at a time. The producer is a local function, and failure is simulated by omitting an event or throwing from that function.
import assert from 'node:assert/strict';
import { EventEmitter, once } from 'node:events';
async function readCycle(bus, start, ms = 1000) {
const controller = new AbortController();
const options = { signal: controller.signal };
const pair = Promise.all([once(bus, 'ready', options),
once(bus, 'done', options)]);
const timer = setTimeout(() => controller.abort(), ms);
try {
return await Promise.all([pair, Promise.resolve().then(start)])
.then(([[[id], [count]]]) => ({ id, count }));
} finally {
clearTimeout(timer);
controller.abort();
}
}
const bus = new EventEmitter();
const result = await readCycle(bus, () => {
bus.emit('ready', 'batch-a');
bus.emit('done', 3);
});
assert.deepEqual(result, { id: 'batch-a', count: 3 });
await assert.rejects(readCycle(bus, () => bus.emit('ready', 'late'), 20),
{ name: 'AbortError' });
await assert.rejects(readCycle(bus, () => { throw new Error('start failed'); }),
/start failed/);
for (const name of ['ready', 'done', 'error']) {
assert.equal(bus.listenerCount(name), 0);
}
console.log(JSON.stringify(result));Run node events.mjs. Expected output:
{"id":"batch-a","count":3}Understand what is bounded
Each once call installs a listener and returns a promise that resolves to an array of event arguments. The two listeners exist before the deferred call to start runs. The array returned by the inner Promise.all is ordered by the promises you supplied, not by which one resolved first. That lets the adapter name the payloads even if done was delivered first. If delivery in that order violates your protocol, add a separate order check rather than assuming this aggregation enforces it.
The timer bounds the event waits. It does not stop the producer, and it cannot interrupt a synchronous call that blocks the event loop. If both events arrive but an asynchronous start promise never settles, this wrapper can still wait on that promise. For a real job, make startup settle or give the producer a separate signal and cancellation contract. The finally cleanup prevents leftover event listeners on failure, but later producer events can still occur.
If a wait expires despite seeing a log from the producer, check whether it emitted before subscription or on a different emitter. Do not share this helper unchanged across concurrent jobs on one emitter. Unkeyed listeners could combine one job's ready with another job's done. Use a dedicated emitter or correlate events by job ID before fulfilling the waits.
Reference: Node.js events.once documentation and multiple-event caveats. More node.js tutorials.