-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
54 lines (51 loc) · 1.35 KB
/
promise.js
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
let promise = new Promise((resolve, reject) => {
let myCountry = 'Ghana'
if (myCountry == 'Ghana'){
resolve('Success')
} else{
reject('Error')
}
})
promise.then((message) => {
console.log(message)
}).catch((message) => {
console.log(message)
})
// Read the countries API using fetch and print the name of country, capital, languages, population and area.
const countriesAPI = 'https://restcountries.com/v2/all'
fetch(countriesAPI)
.then(response => response.json())
.then(data => {
console.log(data)
})
.catch(error => console.error(error))
const fetchData = async () => {
try {
const response = await fetch(countriesAPI)
const countries = await response.json()
console.log(countries)
} catch (err) {
console.error(err)
}
}
console.log('===== async and await')
fetchData
// Print out all the cat names in to catNames variable.
const catsAPI = 'https://api.thecatapi.com/v1/breeds'
fetch(catsAPI)
.then(res => res.json())
.then(data => {
console.log(data)
})
.catch(error => console.error(error))
const fetchInfo = async () => {
try {
const res = await fetch(catsAPI)
const cats = await res.json()
console.log(cats)
} catch (err) {
console.error(err)
}
}
console.log('success')
fetchInfo