renames
This commit is contained in:
parent
787d58a83f
commit
455d300d8d
280 changed files with 2 additions and 2 deletions
|
@ -0,0 +1,29 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
<script>
|
||||
|
||||
let urls = [
|
||||
'https://api.github.com/users/iliakan',
|
||||
'/',
|
||||
'http://no-such-url'
|
||||
];
|
||||
|
||||
Promise.all(
|
||||
urls.map(url => fetch(url).catch(err => err))
|
||||
)
|
||||
.then(responses => Promise.all(
|
||||
// if it's an error then pass on
|
||||
// otherwise response.json() and catch errors as results
|
||||
responses.map(r => r instanceof Error ? r : r.json().catch(err => err))
|
||||
))
|
||||
.then(results => {
|
||||
alert(results[0].name); // Ilya Kantor
|
||||
alert(results[1]); // SyntaxError: Unexpected token < in JSON at position 0
|
||||
alert(results[2]); // TypeError: failed to fetch (text may vary)
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,33 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
<script>
|
||||
|
||||
// the whole promise chain fails with an error here
|
||||
// change that:
|
||||
// make errors appear as members of the results array
|
||||
|
||||
let urls = [
|
||||
'https://api.github.com/users/iliakan',
|
||||
// this URL is HTML page, it's invalid JSON, so response.json() fails
|
||||
'/',
|
||||
// this URL is invalid, so fetch fails
|
||||
'http://no-such-url'
|
||||
];
|
||||
|
||||
// Fix it:
|
||||
Promise.all(urls.map(url => fetch(url)))
|
||||
.then(responses => Promise.all(
|
||||
responses.map(r => r.json())
|
||||
))
|
||||
// Demo output (no need to change):
|
||||
.then(results => {
|
||||
alert(results[0].name); // Ilya Kantor
|
||||
alert(results[1]); // SyntaxError: Unexpected token < in JSON at position 0
|
||||
alert(results[2]); // TypeError: failed to fetch (text may vary)
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,34 @@
|
|||
# Fault-tolerant fetch with JSON
|
||||
|
||||
Improve the solution of the previous task <info:task/promise-errors-as-results>. Now we need not just to call `fetch`, but to load the JSON objects from given URLs.
|
||||
|
||||
Here's the example code to do that:
|
||||
|
||||
```js run
|
||||
let urls = [
|
||||
'https://api.github.com/users/iliakan',
|
||||
'https://api.github.com/users/remy',
|
||||
'https://api.github.com/users/jeresig'
|
||||
];
|
||||
|
||||
// make fetch requests
|
||||
Promise.all(urls.map(url => fetch(url)))
|
||||
// map each response to response.json()
|
||||
.then(responses => Promise.all(
|
||||
responses.map(r => r.json())
|
||||
))
|
||||
// show name of each user
|
||||
.then(users => { // (*)
|
||||
for(let user of users) {
|
||||
alert(user.name);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
The problem is that if any of requests fails, then `Promise.all` rejects with the error, and we loose results of all the other requests. So the code above is not fault-tolerant, just like the one in the previous task.
|
||||
|
||||
Modify the code so that the array in the line `(*)` would include parsed JSON for successful requests and error for errored ones.
|
||||
|
||||
Please note that the error may occur both in `fetch` (if the network request fails) and in `response.json()` (if the response is invalid JSON). In both cases the error should become a member of the results object.
|
||||
|
||||
The sandbox has both of these cases.
|
Loading…
Add table
Add a link
Reference in a new issue