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
+12-12
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ There are 5 static methods in the `Promise` class. We'll quickly cover their use
4
4
5
5
## Promise.all
6
6
7
-
Let's say we want to run many promises to execute in parallel, and wait till all of them are ready.
7
+
Let's say we want to run many promises to execute in parallel, and wait until all of them are ready.
8
8
9
9
For instance, download several URLs in parallel and process the content when all are done.
10
10
@@ -16,7 +16,7 @@ The syntax is:
16
16
let promise =Promise.all([...promises...]);
17
17
```
18
18
19
-
`Promise.all` takes an array of promises (technically can be any iterable, but usually an array) and returns a new promise.
19
+
`Promise.all` takes an array of promises (it technically can be any iterable, but is usually an array) and returns a new promise.
20
20
21
21
The new promise resolves when all listed promises are settled and the array of their results becomes its result.
22
22
@@ -30,7 +30,7 @@ Promise.all([
30
30
]).then(alert); // 1,2,3 when promises are ready: each promise contributes an array member
31
31
```
32
32
33
-
Please note that the order of resulting array members is the same as source promises. Even though the first promise takes the longest time to resolve, it's still first in the array of results.
33
+
Please note that the order of the resulting array members is the same as in its source promises. Even though the first promise takes the longest time to resolve, it's still first in the array of results.
34
34
35
35
A common trick is to map an array of job data into an array of promises, and then wrap that into `Promise.all`.
36
36
@@ -53,7 +53,7 @@ Promise.all(requests)
53
53
));
54
54
```
55
55
56
-
A bigger example with fetching user information for an array of GitHub users by their names (we could fetch an array of goods by their ids, the logic is same):
56
+
A bigger example with fetching user information for an array of GitHub users by their names (we could fetch an array of goods by their ids, the logic is identical):
57
57
58
58
```js run
59
59
let names = ['iliakan', 'remy', 'jeresig'];
@@ -69,7 +69,7 @@ Promise.all(requests)
69
69
70
70
return responses;
71
71
})
72
-
// map array of responses into array of response.json() to read their content
72
+
// map array of responses into an array of response.json() to read their content
Here the second promise rejects in two seconds. That leads to immediate rejection of `Promise.all`, so `.catch` executes: the rejection error becomes the outcome of the whole `Promise.all`.
92
+
Here the second promise rejects in two seconds. That leads to an immediate rejection of `Promise.all`, so `.catch` executes: the rejection error becomes the outcome of the whole `Promise.all`.
93
93
94
94
```warn header="In case of an error, other promises are ignored"
95
95
If one promise rejects, `Promise.all` immediately rejects, completely forgetting about the other ones in the list. Their results are ignored.
@@ -189,11 +189,11 @@ if(!Promise.allSettled) {
189
189
}
190
190
```
191
191
192
-
In this code, `promises.map` takes input values, turns into promises (just in case a non-promise was passed) with `p => Promise.resolve(p)`, and then adds `.then` handler to every one.
192
+
In this code, `promises.map` takes input values, turns them into promises (just in case a non-promise was passed) with `p => Promise.resolve(p)`, and then adds `.then` handler to every one.
193
193
194
194
That handler turns a successful result `v` into `{state:'fulfilled', value:v}`, and an error `r` into `{state:'rejected', reason:r}`. That's exactly the format of `Promise.allSettled`.
195
195
196
-
Then we can use `Promise.allSettled` to get the results or *all* given promises, even if some of them reject.
196
+
Now we can use `Promise.allSettled` to get the results of *all* given promises, even if some of them reject.
197
197
198
198
## Promise.race
199
199
@@ -220,7 +220,7 @@ The first promise here was fastest, so it became the result. After the first set
220
220
221
221
## Promise.resolve/reject
222
222
223
-
Methods `Promise.resolve` and `Promise.reject` are rarely needed in modern code, because `async/await` syntax (we'll cover it in [a bit later](info:async-await)) makes them somewhat obsolete.
223
+
Methods `Promise.resolve` and `Promise.reject` are rarely needed in modern code, because `async/await` syntax (we'll cover it [a bit later](info:async-await)) makes them somewhat obsolete.
224
224
225
225
We cover them here for completeness, and for those who can't use `async/await` for some reason.
226
226
@@ -234,7 +234,7 @@ let promise = new Promise(resolve => resolve(value));
234
234
235
235
The method is used for compatibility, when a function is expected to return a promise.
236
236
237
-
For example, `loadCached` function below fetches URL and remembers (caches) its content. For future calls with the same URL it immediately gets the previous content from cache, but uses `Promise.resolve` to make a promise of it, so that the returned value is always a promise:
237
+
For example, `loadCached` function below fetches a URL and remembers (caches) its content. For future calls with the same URL it immediately gets the previous content from cache, but uses `Promise.resolve` to make a promise of it, so that the returned value is always a promise:
238
238
239
239
```js
240
240
let cache = new Map();
@@ -273,8 +273,8 @@ In practice, this method is almost never used.
273
273
274
274
There are 5 static methods of `Promise` class:
275
275
276
-
1. `Promise.all(promises)` -- waits for all promises to resolve and returns an array of their results. If any of the given promises rejects, then it becomes the error of `Promise.all`, and all other results are ignored.
277
-
2. `Promise.allSettled(promises)` (recently added method) -- waits for all promises to settle and returns their results as array of objects with:
276
+
1. `Promise.all(promises)` -- waits for all promises to resolve and returns an array of their results. If any of the given promises rejects, it becomes the error of `Promise.all`, and all other results are ignored.
277
+
2. `Promise.allSettled(promises)` (recently added method) -- waits for all promises to settle and returns their results as an array of objects with:
278
278
- `state`: `"fulfilled"` or `"rejected"`
279
279
- `value` (if fulfilled) or `reason` (if rejected).
280
280
3. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome.
0 commit comments