Skip to content

Commit 417c38a

Browse files
committed
Updates
1 parent 6f36a02 commit 417c38a

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

control-flow/index.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ This is not what we want. We'll have to go wash our hands again. In production c
2323

2424
Node generally runs in one single thread and any _blocking_ code will be run in sequence, with _non-blocking_ code having the potential to run _asynchronously_.
2525

26-
Thankfully Node offers 3 approaches we can use to control the order in which our code executes. _Callbacks_, _promises_ and _async/await_. Let's take a look at each.
26+
Thankfully Node offers 3 asynchronous approaches we can use to control the order in which our code executes. _Callbacks_, _promises_ and _async/await_. Let's take a look at each.
2727

2828
## Callbacks
2929

@@ -71,7 +71,14 @@ This might be reasonable in simple situations but it's easy for callbacks to get
7171
To show many responses, consider the following code. If we ran them beside each other, the output would not be reliable.
7272

7373
```
74-
// ... replacing the last two lines of the previous example
74+
function randomDelayedResponse(text) {
75+
// Using a timeout here for the sake of simulating an external request
76+
const timeOut = Math.floor(Math.random() * 100) + 1;
77+
const output = text;
78+
setTimeout(() => {
79+
return output;
80+
}, timeOut);
81+
}
7582
7683
randomDelayedResponse(1, console.log); // outputs 1
7784
randomDelayedResponse(2, console.log); // outputs 2
@@ -116,7 +123,7 @@ Let's look at one such alternative.
116123
To avoid this callback hell, we can use a different structure called _promises_. A promise is a function that returns a _resolved_ response which we can chain using `then`, or a _rejected_ response which we can `catch`. It follows this pattern:
117124

118125
```
119-
runOutPromise() // returns a response
126+
runOurPromise() // returns a response
120127
.then(response => {
121128
return foo; // this could optionally be another promise
122129
})
@@ -135,10 +142,10 @@ function randomDelayedResponse(text, callback) {
135142
return new Promise((resolve, reject) => {
136143
const timeOut = Math.floor(Math.random() * 100) + 1;
137144
setTimeout(() => {
138-
if (text) {
139-
resolve(text); // Replacing the callback with "resolve"
145+
if (!text) {
146+
reject('No text provided!'); // Reject when there is no text provided
140147
}
141-
reject('No text provided!'); // Reject when there is an error
148+
resolve(text); // Replacing the callback with "resolve"
142149
}, timeOut);
143150
});
144151
}
@@ -176,7 +183,17 @@ Promises remove the nesting and give us easier to read code. Let's look at a thi
176183
The third approach is built on top of the existing _promises_ approach and results in even simpler code. With `async` and `await` we can write code that feels a lot more like our usual top-down code. It works by telling our commands to wait when we need them to. Let's rewrite our _who will win?_ example from above.
177184

178185
```
179-
// Replace the second part of the example above
186+
function randomDelayedResponse(text, callback) {
187+
return new Promise((resolve, reject) => {
188+
const timeOut = Math.floor(Math.random() * 100) + 1;
189+
setTimeout(() => {
190+
if (!text) {
191+
reject('No text provided!');
192+
}
193+
resolve(text);
194+
}, timeOut);
195+
});
196+
}
180197
181198
async function runTheRace() { // put `async` before the function call
182199
@@ -188,7 +205,14 @@ async function runTheRace() { // put `async` before the function call
188205
console.log(3);
189206
190207
const result4 = await randomDelayedResponse(4);
191-
console.log(result4)
208+
console.log(result4);
209+
210+
// Catching the error
211+
try {
212+
await randomDelayedResponse();
213+
} catch (error) {
214+
console.error(error);
215+
}
192216
}
193217
194218
runTheRace();

0 commit comments

Comments
 (0)