-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello-es6.js
80 lines (70 loc) · 1.75 KB
/
hello-es6.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
// const and let
const name = 'gatuk'
console.log('const name >', name)
let mutableName = ''
mutableName = 'gatuk'
console.log('let mutable name > ', mutableName)
if (mutableName === 'gatuk') {
const name = 'someone'
let mutableName = 'someone'
console.log('inside if scope const name > ', name)
console.log('inside if scope let mutable name > ', mutableName)
}
console.log('outside if scope const name > ', name)
console.log('outside if scope let mutable name > ', mutableName)
// Arrow function & template literal
// function say(name) {
// const greetingMessage = 'Hello ' + name
// console.log(greetingMessage)
// }
// say('Gatuk')
const say = (name) => {
const greetingMessage = `Hello ${name}!`
console.log(greetingMessage)
}
say('Gatuk')
// Destructing and assignment
// Array
const numbers = [1, 2, 3]
const [number1, number2, number3] = numbers
console.log('numbers >', number1, number2, number3)
// object
const user = {
firstname: 'Sudarat',
lastname: 'Chattanon',
}
const { firstname, lastname } = user
console.log('user > ', firstname, lastname)
// callback
function callApi(callback) {
setTimeout(() => {
callback('successfully after 1 sec')
}, 1000)
}
callApi((text) => {
console.log('callback text', text)
})
// promise
const callApiPromise = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('successfully after 300 ms')
}, 300)
})
}
callApiPromise().then((text) => {
console.log('promise text', text)
})
// async/await
const callApiAsync = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('async/await successfully after 500 ms')
}, 300)
})
}
const fetchData = async () => {
const text = await callApiAsync()
console.log(text)
}
fetchData()