Skip to content

Commit f2d678e

Browse files
docs: change order of sections in order to better readable
1 parent 1490ed6 commit f2d678e

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

content/techniques/events.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,29 @@ EventEmitterModule.forRoot({
5050
});
5151
```
5252

53-
#### Event listeners
53+
#### Dispatching Events
54+
55+
To dispatch an event, first inject `EventEmitter2` using standard constructor injection:
56+
57+
```typescript
58+
constructor(private eventEmitter: EventEmitter2) {}
59+
```
60+
61+
> info **Hint** Import the `EventEmitter2` from the `@nestjs/event-emitter` package.
62+
63+
Then use it in a class as follows.
64+
65+
```typescript
66+
this.eventEmitter.emit(
67+
'order.created',
68+
new OrderCreatedEvent({
69+
orderId: 1,
70+
payload: {},
71+
}),
72+
);
73+
```
74+
75+
#### Listening to Events
5476

5577
To declare an event listener, decorate a method with the `@OnEvent()` decorator preceding the method definition containing the code to be executed, as follows:
5678

@@ -79,26 +101,4 @@ handleOrderEvents(payload: OrderCreatedEvent | OrderRemovedEvent | OrderUpdatedE
79101
}
80102
```
81103

82-
#### Dispatching events
83-
84-
To dispatch an event, first inject `EventEmitter2` using standard constructor injection:
85-
86-
```typescript
87-
constructor(private eventEmitter: EventEmitter2) {}
88-
```
89-
90-
> info **Hint** Import the `EventEmitter2` from the `@nestjs/event-emitter` package.
91-
92-
Then use it in a class as follows.
93-
94-
```typescript
95-
this.eventEmitter.emit(
96-
'order.created',
97-
new OrderCreatedEvent({
98-
orderId: 1,
99-
payload: {},
100-
}),
101-
);
102-
```
103-
104104
> info **Hint** `EventEmitter2` class provides several useful methods for interacting with events, like `waitFor` and `onAny`. You can read more about them [here](https://github.com/EventEmitter2/EventEmitter2).

0 commit comments

Comments
 (0)