Skip to content

Commit 7fcd048

Browse files
authored
Merge pull request #1551 from hrodward/patch-34
Update article.md
2 parents 24d3bec + c72735d commit 7fcd048

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ There are 5 static methods in the `Promise` class. We'll quickly cover their use
44

55
## Promise.all
66

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.
88

99
For instance, download several URLs in parallel and process the content when all are done.
1010

@@ -16,7 +16,7 @@ The syntax is:
1616
let promise = Promise.all([...promises...]);
1717
```
1818

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.
2020

2121
The new promise resolves when all listed promises are settled and the array of their results becomes its result.
2222

@@ -30,7 +30,7 @@ Promise.all([
3030
]).then(alert); // 1,2,3 when promises are ready: each promise contributes an array member
3131
```
3232

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.
3434

3535
A common trick is to map an array of job data into an array of promises, and then wrap that into `Promise.all`.
3636

@@ -53,7 +53,7 @@ Promise.all(requests)
5353
));
5454
```
5555

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):
5757

5858
```js run
5959
let names = ['iliakan', 'remy', 'jeresig'];
@@ -69,7 +69,7 @@ Promise.all(requests)
6969

7070
return responses;
7171
})
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
7373
.then(responses => Promise.all(responses.map(r => r.json())))
7474
// all JSON answers are parsed: "users" is the array of them
7575
.then(users => users.forEach(user => alert(user.name)));
@@ -89,7 +89,7 @@ Promise.all([
8989
]).catch(alert); // Error: Whoops!
9090
```
9191

92-
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`.
9393

9494
```warn header="In case of an error, other promises are ignored"
9595
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) {
189189
}
190190
```
191191
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.
193193
194194
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`.
195195
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.
197197
198198
## Promise.race
199199
@@ -220,7 +220,7 @@ The first promise here was fastest, so it became the result. After the first set
220220
221221
## Promise.resolve/reject
222222
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.
224224
225225
We cover them here for completeness, and for those who can't use `async/await` for some reason.
226226
@@ -234,7 +234,7 @@ let promise = new Promise(resolve => resolve(value));
234234
235235
The method is used for compatibility, when a function is expected to return a promise.
236236
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:
238238
239239
```js
240240
let cache = new Map();
@@ -273,8 +273,8 @@ In practice, this method is almost never used.
273273
274274
There are 5 static methods of `Promise` class:
275275
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:
278278
- `state`: `"fulfilled"` or `"rejected"`
279279
- `value` (if fulfilled) or `reason` (if rejected).
280280
3. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome.

0 commit comments

Comments
 (0)