-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.html
71 lines (52 loc) · 2.09 KB
/
fetch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html>
<head>
<!-- Load babel 5 - babel-core -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.38/browser.js"></script>
<script type="text/babel">
fetch('http://api.open-notify.org/astros.json')
.then(res => res.json())
.then(console.log);
// Use fetch within a function
const getPeopleInSpace = () =>
fetch('http://api.open-notify.org/astros.json')
.then(res => res.json());
// Only fetch people's names
const spaceNames = () =>
getPeopleInSpace()
.then(json => json.people)
.then(people => people.map( p => p.name))
.then(names => names.join(', '));
spaceNames()
.then(console.log);
// const spacePeople = () => {
// return new Promise((resolves, rejects) => {
// const api = 'http://api.open-notify.org/astros.json';
// const request = new XMLHttpRequest();
// request.open('GET', api);
// request.onload = () => {
// if (request.status === 200) {
// resolves(JSON.parse(request.response));
// }else {
// rejects(Error(request.statusText));
// }
// };
// request.onerror = error => rejects(err);
// request.send();
// });
// };
// spacePeople().then(
// spaceData => console.log(spaceData),
// err => console.error(
// new Error('Cannot load new space people')
// )
// );
</script>
<title>Fetch</title>
</head>
<body>
<h1>Hit the pit</h1>
<h2>Fetch</h2>
<p>Using fetch to simplify the building promises process</p>
</body>
</html>