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