Skip to content

Commit 29292c1

Browse files
committed
minor
1 parent b9bfba0 commit 29292c1

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

1-js/11-async/05-promise-api/article.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function loadCached(url) {
3535
return fetch(url)
3636
.then(response => response.text())
3737
.then(text => {
38-
cache.set(url,text);
38+
cache.set(url,text);
3939
return text;
4040
});
4141
}
@@ -63,23 +63,29 @@ We cover it here for completeness, rarely used in real code.
6363

6464
## Promise.all
6565

66-
The method to run many promises in parallel and wait till all of them are ready.
66+
Let's say we want to run many promises to execute in parallel, and wait till all of them are ready.
67+
68+
For instance, download several urls in parallel and process the content when all done.
69+
70+
That's what `Promise.all` is for.
6771

6872
The syntax is:
6973

7074
```js
71-
let promise = Promise.all(iterable);
75+
let promise = Promise.all([...promises...]);
7276
```
7377

74-
It takes an `iterable` object with promises, technically it can be any iterable, but usually it's an array, and returns a new promise. The new promise resolves with when all of them are settled and has an array of their results.
78+
It takes an array of promises (technically can be any iterable, but usually an array) and returns a new promise.
79+
80+
The new promise resolves when all listed promises are settled and has an array of their results.
7581

7682
For instance, the `Promise.all` below settles after 3 seconds, and then its result is an array `[1, 2, 3]`:
7783

7884
```js run
7985
Promise.all([
80-
new Promise((resolve, reject) => setTimeout(() => resolve(1), 3000)), // 1
81-
new Promise((resolve, reject) => setTimeout(() => resolve(2), 2000)), // 2
82-
new Promise((resolve, reject) => setTimeout(() => resolve(3), 1000)) // 3
86+
new Promise(resolve => setTimeout(() => resolve(1), 3000)), // 1
87+
new Promise(resolve => setTimeout(() => resolve(2), 2000)), // 2
88+
new Promise(resolve => setTimeout(() => resolve(3), 1000)) // 3
8389
]).then(alert); // 1,2,3 when promises are ready: each promise contributes an array member
8490
```
8591

@@ -132,7 +138,6 @@ If any of the promises is rejected, `Promise.all` immediately rejects with that
132138

133139
For instance:
134140

135-
136141
```js run
137142
Promise.all([
138143
new Promise((resolve, reject) => setTimeout(() => resolve(1), 1000)),
@@ -149,8 +154,8 @@ The important detail is that promises provide no way to "cancel" or "abort" thei
149154

150155
There are ways to avoid this: we can either write additional code to `clearTimeout` (or otherwise cancel) the promises in case of an error, or we can make errors show up as members in the resulting array (see the task below this chapter about it).
151156

152-
````smart header="`Promise.all(iterable)` allows non-promise items in `iterable`"
153-
Normally, `Promise.all(iterable)` accepts an iterable (in most cases an array) of promises. But if any of those objects is not a promise, it's wrapped in `Promise.resolve`.
157+
````smart header="`Promise.all(...)` allows non-promise items in `iterable`"
158+
Normally, `Promise.all(...)` accepts an iterable (in most cases an array) of promises. But if any of those objects is not a promise, it's wrapped in `Promise.resolve`.
154159

155160
For instance, here the results are `[1, 2, 3]`:
156161

0 commit comments

Comments
 (0)