-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
57 lines (47 loc) · 1.29 KB
/
solution.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
const https = require('https')
const makePokeUrl = pokemon => `https://pokeapi.co/api/v2/pokemon/${pokemon}/`
const pikaUrl = makePokeUrl('pikachu')
// challenge 1
const myPromiseApi = url => {
return new Promise((resolve, reject) => {
https
.get(url, resp => {
let data = ''
resp.on('data', chunk => {
data += chunk
})
resp.on('end', () => {
try {
resolve(JSON.parse(data))
} catch (e) {
reject('It dun broked')
}
})
})
.on('error', err => {
reject(err.message)
})
})
}
myPromiseApi(pikaUrl)
.then(console.log)
.catch(console.log)
// challenge 2
const pikaPromise = myPromiseApi(pikaUrl)
const itemPromise = jsonData => {
const itemUrl = jsonData.held_items[0].item.url
return myPromiseApi(itemUrl)
}
pikaPromise
.then(itemPromise)
.then(console.log)
.catch(console.log)
// challenge 3
const charUrl = makePokeUrl('charmander')
const squirtUrl = makePokeUrl('squirtle')
const bulbaUrl = makePokeUrl('bulbasaur')
const charPromise = myPromiseApi(charUrl)
const squirtPromise = myPromiseApi(squirtUrl)
const bulbaPromise = myPromiseApi(bulbaUrl)
const promiseArray = [charPromise, squirtPromise, bulbaPromise]
Promise.all(promiseArray).then(console.log)