Skip to content

Commit ce60226

Browse files
committed
docs: update README to include synchronous and asynchronous event emission methods in TransactionalEventEmitter
1 parent be3b3af commit ce60226

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

packages/core/README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,35 @@ The module uses a `TransactionalEventEmitter` for reliable event emission. This
127127
}]
128128
);
129129
```
130-
- **Event Contract:** Ensure that your event classes implement the `InboxOutboxEvent` interface for consistency and clarity.
130+
131+
### Synchronous vs Asynchronous Emission
132+
133+
The `TransactionalEventEmitter` provides two methods for emitting events:
134+
135+
- **emit:**
136+
- Fires the event and attempts immediate delivery to listeners, but does **not** wait for listeners to finish execution.
137+
- Use this when you want to trigger event delivery and continue your logic without waiting for listeners to complete.
138+
139+
- **emitAsync:**
140+
- Fires the event and **waits for all listeners to finish execution** before resolving.
141+
- Use this when you need to ensure that all listeners have processed the event before proceeding (e.g., for transactional workflows or when listener side effects are required before continuing).
142+
143+
**Example:**
144+
```typescript
145+
// Wait for all listeners to finish processing the event
146+
await this.transactionalEventEmitter.emitAsync(
147+
new UserApplicationAssignedEvent(user.token, application.token, userApplication.token),
148+
[{
149+
entity: userApplication,
150+
operation: TransactionalEventEmitterOperations.persist,
151+
}]
152+
);
153+
```
154+
155+
> **Note:** Use `emitAsync` if you need to wait for listeners to execute and complete before moving on. Use `emit` if you want to fire-and-forget the event delivery.
156+
157+
### Event Contract:
158+
Ensure that your event classes implement the `InboxOutboxEvent` interface for consistency and clarity.
131159

132160
### Module Registration
133161

0 commit comments

Comments
 (0)