-
Notifications
You must be signed in to change notification settings - Fork 83
/
07_async.js
60 lines (51 loc) · 1.16 KB
/
07_async.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
// 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
]))
// 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()