-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync-basic-concept.js
58 lines (39 loc) · 1.34 KB
/
async-basic-concept.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
// info:
// This is a basic concept of async functions.
// You can see how asyc are a sequenzial tasks except
// that one with await. In that one case, the stack of that function
// into the event loop is detached and frozen waiting for
// its result and all other tasks slide on end of stack.
// Async respect its scope, all other functions run, but
// the one with with that await is in "async status"
const first_async = async () => {
console.log('- first 1')
console.log('- first 2')
}
const middle_async = async () => {
console.log('- mid 0 ...without an await not exist a stop into the stack, async it s a simple "wait a result" ')
await console.log('- mid 1 ...wait result in this func, do other func.')
console.log('- mid 2 ...moved last into the stack')
console.log('- mid 3 ...moved last into the stack')
}
const last_async = async () => {
console.log('- last 1')
console.log('- last 2')
}
/* --- */
first_async()
console.log('----------')
middle_async()
console.log('----------')
last_async()
// log:
// - first 1
// - first 2
// ----------
// - mid 0 ...without an await not exist a stop into the stack, async it s a simple "wait a result"
// - mid 1 ...wait result in this func, do other func.
// ----------
// - last 1
// - last 2
// - mid 2 ...moved last into the stack
// - mid 3 ...moved last into the stack