-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-JavaScript Promises -- Tutorial for Beginners.js
98 lines (82 loc) · 2.17 KB
/
12-JavaScript Promises -- Tutorial for Beginners.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
// function getWeather() {
// return new Promise(function(resolve, reject) {
// setTimeout(() => {
// resolve('Sunny')
// }, 100)
// })
// }
// function getWeatherIcon(weather) {
// return new Promise(function(resolve, reject) {
// setTimeout(() => {
// switch(weather) {
// case 'Sunny':
// resolve('☀️')
// break
// case 'Cloudy':
// resolve('☁️')
// break
// case 'Rainy':
// resolve('🌧️')
// break
// default:
// reject('NO ICON FOUND')
// }
// }, 100)
// })
// }
// function onSuccess(data) {
// console.log(`Success ${data}`)
// }
// function onError(error) {
// console.log(`Error: ${error}`)
// }
// getWeather()
// .then(getWeatherIcon())
// .then(onSuccess, onError)
//-----------------------------------
// function fun1() {
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// // resolve('Good data')
// reject('BAD DATA')
// }, 100)
// })
// }
// function fun2() {
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve('🥸')
// }, 100)
// })
// }
// function onSuccess(data) {
// console.log(`Success: ${data}`)
// }
// function onError(errorCode) {
// console.log(`ERROR: ${errorCode}`)
// }
// function onFinally() {
// console.log('FINALLY WE BE DONE YO')
// }
// fun1()
// .then(fun2)
// .then(onSuccess)
// .catch(onError)
// .finally(onFinally)
//--------------------------
function fetchData() {
return new Promise(function(resolve,reject) {
fetch('https://api.weather.gov/gridpoints/OKX/35,35/forecast')
.then(response => response.json())
.then(data => resolve(data.properties.periods[1].shortForecast));
})
}
function displayData(weather) {
console.log(weather)
}
function onError(err) {
console.log(`ERROR: ${err}`)
}
fetchData()
.then(displayData)
.catch(onError)