How to understand event-driven code: emitters, queues and subscribers
Event-driven code breaks the call chain: the code that emits an event doesn't call the code that handles it. How to find and follow events, queues and webhooks.
In event-driven code, one part of the system announces that something happened, and other parts react. That decoupling is useful for design and hard for reading: there's no direct call from the emitter to the handler, so "find all references" and stack traces don't connect them.
Recognise the patterns
- In-process emitters: Node's
EventEmitter,emitter.on('order.placed', ...)andemitter.emit('order.placed', ...) - Framework events: NestJS's event emitter, Medusa subscribers, model hooks in ORMs
- Job queues: BullMQ, SQS, Cloud Tasks: one part adds a job, a worker processes it
- Message brokers: Kafka, RabbitMQ, Redis pub/sub, NATS
- Webhooks: external services call your endpoints when something happens on their side
- Browser events: custom DOM events, message passing between frames or extension contexts
Follow the event name
Event names are usually strings or constants. The most reliable technique is simple:
- Find the emit (
emit,publish,add,send). - Note the event name or queue name.
- Search the whole codebase for that name to find every subscriber.
If names are built dynamically ("order." + status), search for the prefix and read carefully.
Build an event map
| Event | Emitted by | Handled by |
|---|---|---|
| order.placed | services/checkout.ts:120 | subscribers/send-receipt.ts, subscribers/update-stock.ts |
| payment.failed | webhooks/stripe.ts:44 | subscribers/notify-customer.ts |
This table is worth keeping in the repository's docs.
Understand delivery guarantees
- In-process events run in the same process, often synchronously. A slow handler slows the emitter.
- Queues usually deliver at least once, so handlers may run twice and must be idempotent.
- Ordering may not be guaranteed. Two events about the same record can arrive out of order.
- Failures may be retried, sent to a dead-letter queue or dropped. Find out which.
Find where workers run
Queue workers often run in a separate process with its own entry point and deployment. Check package.json scripts and deployment configs for a worker command. A handler that never runs may simply be in a process that isn't deployed.
Trace a chain
Events can trigger events. A placed order sends a receipt, which logs an email event, which updates analytics. Follow at least one chain fully to understand how far a single action reaches.
Ask across the gap
Ask RepoGrasp "what happens after an order is placed? List every subscriber and job that reacts, with citations." It can connect emitters and handlers by reading the event names across files, which is exactly what static references can't do. Confirm by searching for each event name.