-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-promises.js
101 lines (82 loc) · 2.92 KB
/
06-promises.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var XMLHttpRequest = require('xhr2');
// Callback
const getDataCallback = (callback) => {
setTimeout(() => {
callback('This is my callback error', undefined)
callback(undefined, 'This is my callback data')
}, 2000)
}
getDataCallback((err, data) => {
if (err) {
console.log(err)
} else {
console.log(data)
}
})
// Promise - it is an easier alternative to callbacks
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
// resolve('This is the promise data')
reject('This is my promise error')
reject('This is my promise error')
}, 2000)
})
myPromise.then((data) => {
console.log(data)
}, (err) => {
console.log(err)
})
myPromise.then((data) => {
console.log(data)
}, (err) => {
console.log(err)
})
//----------------------------------------------------------------------------------------------------------------------
//below is an even better way using promises than callbacks:
//we are getting 429 error as we are sending too many requests.. it is ok to ignore but both the above and below calls work just fine
const getCountry = (countryCode) => new Promise((resolve, reject) => {
const countryRequest = new XMLHttpRequest()
countryRequest.addEventListener('readystatechange', (e) => {
if (e.target.readyState === 4 && e.target.status === 200) {
const data = JSON.parse(e.target.responseText)
const country = data.find((country) => country.alpha2Code === countryCode)
resolve(country)
} else if (e.target.readyStatet === 4) {
reject('Unable to fetch data')
}
})
countryRequest.open('GET', 'http://api.countrylayer.com/v2/all?access_key=87728d65a586a03a2aa381d8e3403a19')
countryRequest.send()
})
getCountry('MX').then((country) => {
console.log('got the country',country.name)
}, (err) => {
console.log(`Error: ${err}`)
})
//if we use 2 callbacks for doing 2 async things, it becomes harder with callbacks. promise chaining will be a better method to do such things
//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----
//promise chaining
const getDataPromise = (num) => new Promise((resolve, reject) => {
setTimeout(() => {
typeof num === 'number' ? resolve(num * 2) : reject('Number must be provided')
}, 2000)
})
getDataPromise(2).then((data) => {
getDataPromise(data).then((data) => {
console.log(`Promise data: ${data}`)
}, (err) => {
console.log(err)
})
}, (err) => {
console.log(err)
})
getDataPromise('10').then((data) => {
return getDataPromise(data)
}).then((data) => {
return getDataPromise(data)
}).then((data) => {
console.log(data)
}).catch((err) => {
console.log(err)
})
//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----