-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-fetch.mjs
More file actions
30 lines (25 loc) · 856 Bytes
/
08-fetch.mjs
File metadata and controls
30 lines (25 loc) · 856 Bytes
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
//fetch wasnt available in node.. installed using 'npm install node-fetch'
//we need to import it as below.. and if we import it as below, it has to be a module. so named the file with a .mjs extension for module javascript
import fetch from 'node-fetch'
fetch('http://puzzle.mead.io/puzzle', {}).then((response) => {
if (response.status === 200) {
return response.json()
} else {
throw new Error('Unable to fetch the puzzle')
}
}).then((data) => {
console.log(data.puzzle)
}).catch((error) => {
console.log(error)
})
fetch('http://baaaaad.io/puzzle', {}).then((response) => {
if (response.status === 200) {
return response.json()
} else {
throw new Error('Unable to fetch the puzzle')
}
}).then((data) => {
console.log(data.puzzle)
}).catch((error) => {
console.log(error)
})