This commit is contained in:
Ilya Kantor 2019-08-09 23:03:18 +03:00
parent e82885055b
commit 207fbe848f
3 changed files with 51 additions and 40 deletions

View file

@ -1,10 +1,9 @@
To fetch a user we need:
To fetch a user we need: `fetch('https://api.github.com/users/USERNAME')`.
1. `fetch('https://api.github.com/users/USERNAME')`.
2. If the response has status `200`, call `.json()` to read the JS object.
If the response has status `200`, call `.json()` to read the JS object.
If a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting arrray.
Otherwise, if a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting arrray.
So here's the code:
@ -38,4 +37,4 @@ Please note: `.then` call is attached directly to `fetch`, so that when we have
If we used `await Promise.all(names.map(name => fetch(...)))`, and call `.json()` on the results, then it would wait for all fetches to respond. By adding `.json()` directly to each `fetch`, we ensure that individual fetches start reading data as JSON without waiting for each other.
That's an example of how low-level `Promise` API can still be useful even if we mainly use `async/await`.
That's an example of how low-level Promise API can still be useful even if we mainly use `async/await`.

View file

@ -8,5 +8,6 @@ There's a test example in the sandbox.
Important details:
1. There should be one `fetch` request per user. And requests shouldn't wait for each other. So that the data arrives as soon as possible.
2. If any request fails, or if there's no such user, the function should return `null` in the resulting array.
1. There should be one `fetch` request per user.
2. Requests shouldn't wait for each other. So that the data arrives as soon as possible.
3. If any request fails, or if there's no such user, the function should return `null` in the resulting array.