-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13-Async Await vs. Promises - JavaScript Tutorial for beginners.js
88 lines (72 loc) · 1.84 KB
/
13-Async Await vs. Promises - JavaScript 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
// function getData() {
// return new Promise(resolve => {
// setTimeout(() => {
// resolve(46)
// }, 1)
// })
// }
// async function start() {
// const result = await getData()
// console.log(result)
// }
// async function start2() {
// getData()
// .then(result => {
// console.log(result)
// })
// }
// start()
//-----------------------------------
// async function start() {
// const data = await fetch('https://api.weather.gov/gridpoints/OKX/35,53/forecast')
// const result = await data.json()
// console.log(result.properties.periods[1].shortForecast)
// }
// async function start2() {
// fetch('https://api.weather.gov/gridpoints/OKX/35,53/forecast')
// .then(data => data.json())
// .then(result => {
// console.log(result.properties.periods[1].shortForecast)
// })
// }
// start()
//-----------------------------------
// function getData() {
// return new Promise(function(resolve, reject) {
// setTimeout(() => {
// reject('Something went wrong!')
// }, 1)
// })
// }
// async function start() {
// try {
// const result = await getData()
// } catch (error) {
// console.log(`ERROR: ${error}`)
// }
// }
// async function start2() {
// const result = await getData()
// .catch(error => {
// console.log(`ERROR: ${error}`)
// })
// }
// start2()
//-----------------------------------
function getData() {
return new Promise(function(resolve, reject) {
setTimeout(() => {
// resolve('KHERE is your DATA')
reject('Something went wrong!')
}, 1)
})
}
async function start() {
try {
const result = await getData()
// SUCCESS
} catch (error) {
// FAILURE
}
}
start()