-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasync.ts
50 lines (39 loc) · 1010 Bytes
/
async.ts
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
// * ------------------------------------------------ async await
{
const timeout = async (ms) => await new Promise((resolve) => setTimeout(resolve, ms));
const print = async () => {
await timeout(100);
console.log('Async Hello');
await timeout(200);
console.log('Async Again');
};
print();
}
// * ------------------------------------------------ Promise
{
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const print = () => {
timeout(100)
.then(() => {
console.log('Promise Hello');
})
.then(() => timeout(200))
.then(() => {
console.log('Promise Again');
});
};
print();
}
// * ------------------------------------------------ Callback
{
const timeout = (ms, callback) => setTimeout(callback, ms);
const print = () => {
timeout(100, () => {
console.log('Callback Hello');
timeout(200, () => {
console.log('Callback Again');
});
});
};
print();
}