You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: behavioral/observer/readme.md
+35-39
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,8 @@ class Observable {
61
61
62
62
> We could call the listener with `this` instead of `null` in case we need to have access the observable within the listener's code via the `this` operator.
63
63
64
+
Bear in mind that this method should always be called from an asynchronous context, for instance an operation of the observable running asynchronously.
65
+
64
66
### An observable must have a purpose
65
67
66
68
An observable must have a purpose, it must provide at least one asynchronous `operation`. It doesn't have to be an asynchronous operation, even though it only makes sense to have an asynchronous observable. Within each operation the observable must use the `emit` method in order to call every listener given this event type along with any data arguments.
@@ -69,21 +71,19 @@ An observable must have a purpose, it must provide at least one asynchronous `op
69
71
classObservable {
70
72
...
71
73
72
-
operation () {
73
-
setTimeout(() => {
74
-
// Execute any business logic
75
-
constvalueA=...
76
-
constvalueB=...
77
-
...
78
-
79
-
// Emit the success of the operation
80
-
this.emit("success", valueA, valueB, ...);
81
-
});
74
+
asyncoperation () {
75
+
// Execute any business logic
76
+
constvalueA=await...
77
+
constvalueB=await...
78
+
...
79
+
80
+
// Emit the success of the operation
81
+
this.emit("success", valueA, valueB, ...);
82
82
}
83
83
}
84
84
```
85
85
86
-
> We are using the `setTimeout` function to mimic the execution of an asynchronous operation.
86
+
> Operation is set to be an async function in order to call operations asynchronously.
87
87
88
88
### Register event listeners
89
89
@@ -106,20 +106,18 @@ A special care must be taken regarding the error handling in the observer patter
106
106
classObservable {
107
107
...
108
108
109
-
operation () {
110
-
setTimeout(() => {
111
-
try {
112
-
// Execute any business logic
113
-
constvalueA=...
114
-
constvalueB=...
115
-
...
116
-
117
-
// Emit the success of the operation
118
-
this.emit("success", valueA, valueB, ...);
119
-
} catch (error) {
120
-
this.emit("error", error); // Emit the thrown error
121
-
}
122
-
});
109
+
asyncoperation () {
110
+
try {
111
+
// Execute any business logic
112
+
constvalueA=await...
113
+
constvalueB=await...
114
+
...
115
+
116
+
// Emit the success of the operation
117
+
this.emit("success", valueA, valueB, ...);
118
+
} catch (error) {
119
+
this.emit("error", error); // Emit the thrown error
120
+
}
123
121
}
124
122
}
125
123
```
@@ -161,20 +159,18 @@ class Observable {
161
159
}
162
160
}
163
161
164
-
operation () {
165
-
setTimeout(() => {
166
-
try {
167
-
// Execute any business logic
168
-
constvalueA=...
169
-
constvalueB=...
170
-
...
171
-
172
-
// Emit the success of the operation
173
-
this.emit("success", valueA, valueB, ...);
174
-
} catch (error) {
175
-
this.emit("error", error); // Emit the thrown error
176
-
}
177
-
});
162
+
asyncoperation () {
163
+
try {
164
+
// Execute any business logic
165
+
constvalueA=await...
166
+
constvalueB=await...
167
+
...
168
+
169
+
// Emit the success of the operation
170
+
this.emit("success", valueA, valueB, ...);
171
+
} catch (error) {
172
+
this.emit("error", error); // Emit the thrown error
0 commit comments