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: 1-js/11-async/05-promise-api/article.md
+15-10Lines changed: 15 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,7 @@ function loadCached(url) {
35
35
returnfetch(url)
36
36
.then(response=>response.text())
37
37
.then(text=> {
38
-
cache.set(url,text);
38
+
cache.set(url,text);
39
39
return text;
40
40
});
41
41
}
@@ -63,23 +63,29 @@ We cover it here for completeness, rarely used in real code.
63
63
64
64
## Promise.all
65
65
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.
67
71
68
72
The syntax is:
69
73
70
74
```js
71
-
let promise =Promise.all(iterable);
75
+
let promise =Promise.all([...promises...]);
72
76
```
73
77
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.
75
81
76
82
For instance, the `Promise.all` below settles after 3 seconds, and then its result is an array `[1, 2, 3]`:
@@ -149,8 +154,8 @@ The important detail is that promises provide no way to "cancel" or "abort" thei
149
154
150
155
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).
151
156
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`.
0 commit comments