Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit c0f164f

Browse files
committed
Adding simple example of series vs parallel
1 parent 4a8d6f7 commit c0f164f

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/content/en/fundamentals/getting-started/primers/async-functions.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,31 @@ Note: I'm sort-of in love with streams. If you're unfamiliar with streaming,
196196
guide](https://jakearchibald.com/2016/streams-ftw/#streams-the-fetch-api){:
197197
.external}.
198198

199-
## Example: Avoid going too sequential
199+
## Careful! Avoid going too sequential
200+
201+
Although you're writing code that looks synchronous, ensure you don't miss the
202+
opportunity to do things in parallel.
203+
204+
async function series() {
205+
await wait(500);
206+
await wait(500);
207+
return "done!";
208+
}
209+
210+
The above takes 1000ms to complete, whereas:
211+
212+
async function parallel() {
213+
const wait1 = wait(500);
214+
const wait2 = wait(500);
215+
await wait1;
216+
await wait2;
217+
return "done!";
218+
}
219+
220+
…the above takes 500ms to complete, because both waits happen at the same time.
221+
Let's look at a practical example…
222+
223+
### Example: Outputting fetches in order
200224

201225
Say we wanted to fetch a series URLs and log them as soon as possible, in the
202226
correct order.
@@ -312,6 +336,10 @@ code-bloat.
312336

313337
## Async all the things!
314338

315-
Once async functions land across all browsers, use them on every promise-returning function! Not only do they make your code tider, but it makes sure that function will *always* return a promise.
339+
Once async functions land across all browsers, use them on every
340+
promise-returning function! Not only do they make your code tider, but it makes
341+
sure that function will *always* return a promise.
316342

317-
I got really excited about async functions [back in 2014](https://jakearchibald.com/2014/es7-async-functions/){: .external}, and it's great to see them land, for real, in browsers. Whoop!
343+
I got really excited about async functions [back in
344+
2014](https://jakearchibald.com/2014/es7-async-functions/){: .external}, and
345+
it's great to see them land, for real, in browsers. Whoop!

0 commit comments

Comments
 (0)