-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_async.js
More file actions
63 lines (50 loc) · 1.29 KB
/
07_async.js
File metadata and controls
63 lines (50 loc) · 1.29 KB
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
// Event Loop
// const timeout = setTimeout(() => {
// console.log('After timeout')
// }, 2500)
// clearTimeout(timeout)
// const interval = setInterval(() => {
// console.log('After timeout')
// }, 1000)
// clearInterval(interval)
// const delay = (callback, wait = 1000) => {
// setTimeout(callback, wait)
// }
// delay(() => {
// console.log('After 2 seconds')
// }, 2000)
const delay = (wait = 1000) => {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
// resolve()
reject('Программа наебнулась, дружище... Попробуй снова!')
}, wait)
})
return promise
}
// delay(2500)
// .then(() => {
// console.log('After 2 seconds')
// })
// .catch((err) => {
// console.error('Error:', err)
// })
// .finally(() => {
// console.log('Finally')
// })
const getData = () => new Promise(resolve => resolve([
1, 1, 2, 3, 5, 8, 13, 21
]))
// getData().then(data => console.log(data))
async function asyncExample() {
try {
await delay(3000)
const data = await getData()
console.log('Data', data)
} catch (e) {
console.log(e)
} finally {
console.log('Finally :)')
}
}
asyncExample()