Skip to content

Commit 6efd80f

Browse files
committed
Use async/await instead of setTimeout
1 parent 3ad0c71 commit 6efd80f

File tree

2 files changed

+56
-63
lines changed

2 files changed

+56
-63
lines changed

behavioral/observer/readme.md

+35-39
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class Observable {
6161

6262
> 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.
6363
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+
6466
### An observable must have a purpose
6567

6668
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
6971
class Observable {
7072
...
7173

72-
operation () {
73-
setTimeout(() => {
74-
// Execute any business logic
75-
const valueA = ...
76-
const valueB = ...
77-
...
78-
79-
// Emit the success of the operation
80-
this.emit("success", valueA, valueB, ...);
81-
});
74+
async operation () {
75+
// Execute any business logic
76+
const valueA = await ...
77+
const valueB = await ...
78+
...
79+
80+
// Emit the success of the operation
81+
this.emit("success", valueA, valueB, ...);
8282
}
8383
}
8484
```
8585

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.
8787
8888
### Register event listeners
8989

@@ -106,20 +106,18 @@ A special care must be taken regarding the error handling in the observer patter
106106
class Observable {
107107
...
108108

109-
operation () {
110-
setTimeout(() => {
111-
try {
112-
// Execute any business logic
113-
const valueA = ...
114-
const valueB = ...
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+
async operation () {
110+
try {
111+
// Execute any business logic
112+
const valueA = await ...
113+
const valueB = 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+
}
123121
}
124122
}
125123
```
@@ -161,20 +159,18 @@ class Observable {
161159
}
162160
}
163161

164-
operation () {
165-
setTimeout(() => {
166-
try {
167-
// Execute any business logic
168-
const valueA = ...
169-
const valueB = ...
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+
async operation () {
163+
try {
164+
// Execute any business logic
165+
const valueA = await ...
166+
const valueB = 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
173+
}
178174
}
179175
}
180176

behavioral/observer/thermometer.js

+21-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { sensors } from "iot";
2+
13
class Thermometer {
24
constructor() {
5+
this.running = false;
36
this.previous = 0;
47
this.temperature = 0;
58

@@ -30,11 +33,13 @@ class Thermometer {
3033
}
3134
}
3235

33-
update (value) {
34-
setTimeout(() => {
36+
async start () {
37+
this.running = true;
38+
39+
while (this.running) {
3540
try {
3641
this.previous = this.temperature;
37-
this.temperature = value;
42+
this.temperature = await sensors.readTemperature();
3843

3944
const delta = this.temperature - this.previous;
4045

@@ -46,7 +51,11 @@ class Thermometer {
4651
} catch (error) {
4752
this.emit("error", error);
4853
}
49-
});
54+
}
55+
}
56+
57+
stop () {
58+
this.running = false;
5059
}
5160

5261
removeListener (event, listener) {
@@ -61,34 +70,22 @@ class Thermometer {
6170

6271
const t = new Thermometer();
6372

64-
t.update(10);
73+
t.start();
6574

6675
t.on("increase", (temp, delta) => {
67-
console.log(`Temperature increased up to ${temp} with a delta ${delta}`)
76+
console.log({ temp, delta });
6877
});
6978

7079
t.on("decrease", (temp, delta) => {
71-
console.log(`Temperature decreased to ${temp} with a delta ${delta}`)
80+
console.log({ temp, delta });
7281
});
7382

7483
t.on("error", (error) => {
75-
console.error(`An error occurred updating the temperature: ${error.message}`)
84+
console.error(`Error: ${error.message}`)
7685
});
7786

78-
t.on("increase", (temp, delta) => {
79-
console.log(`With the temperature increased by ${delta} is getting warmer`)
80-
});
81-
82-
t.update(-15);
83-
t.update(8);
84-
t.update(8);
85-
t.update(40);
86-
8787
// Async output:
88-
// Temperature increased up to 10 with a delta 10
89-
// With the temperature increased by 10 is getting warmer
90-
// Temperature decreased to -15 with a delta -25
91-
// Temperature increased up to 8 with a delta 23
92-
// With the temperature increased by 23 is getting warmer
93-
// Temperature increased up to 40 with a delta 32
94-
// With the temperature increased by 32 is getting warmer
88+
// { temp: -1, delta: -1 }
89+
// { temp: -3, delta: -2 }
90+
// { temp: -5, delta: -2 }
91+
// { temp: -7, delta: -2 }

0 commit comments

Comments
 (0)