33 lines
862 B
HTML
33 lines
862 B
HTML
<!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>
|