-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_syntax.js
111 lines (89 loc) · 3.57 KB
/
async_syntax.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// synchronous
responseA = fetchFromServiceA()
response_B = fetchFromServiceB(responseA.data)
finallyProcess(responseB.data)
responseC = fetchFromServiceC()
responseD = fetchFromServiceD(responseC.data)
finallyProcess(responseD.data)
// explicit threads
function ABfunction() {
responseA = fetchFromServiceA()
response_B = fetchFromServiceB(responseA.data)
finallyProcess(responseB.data)
}
function CDfunction() {
responseC = fetchFromServiceC()
responseD = fetchFromServiceD(responseC.data)
finallyProcess(responseD.data)
}
makeThread(ABfunction).run()
makeThread(CDfunction).run()
// event loop
function runEventLoopUntilAllTasksCompleted(tasks) {
while (tasks) {
task = selectTaskWhichCanMakeForwardProgress(tasks)
task.executeSomeInstructionsUntilWaitingOnIO()
if task.isComplete() {
tasks.remove(task)
}
}
}
runEventLoopUntilAllTasksCompleted([ABTask, CDTask])
// --------------------------------------------------------------------------------------------
// callbacks
fetchFromServiceA(args, callbackA) // At some point this makes a call to socket.read(), in order
// to read the response. But the socket is in non-blocking mode.
// What happens at this point is a task containing a reference to
// callbackA is added to a task queue.
callbackA = (responseA) =>
fetchFromServiceB(responseA.data, callbackB) // Now, some time has passed and we are executing the
// task that was added to the task queue. A second
// task is enqueued, this time with a reference to
// callbackB
callbackB = (responseB) => finally_process(responseB)
fetchFromServiceC(args, callbackC)
callbackD = (responseD) => finally_process(responseD)
callbackC = (responseC) =>
fetchFromServiceD(responseC.data, callbackD)
callbackA = function(responseA) {
fetchFromServiceB(responseA.data, callbackB)
}
callbackB = function(responseB) {
finally_process(responseB)
}
fetchFromServiceA(data, callbackA)
fetchFromServiceA(data, function(responseA) {
fetchFromServiceB(responseA.data, function(responseB) {
finally_process(responseB)
})
})
// simplified anonymous function syntax
fetch(args, (response_1) =>
fetch_using_response_1(response_1.data, (response_2) =>
fetch_using_response_2(response_2.data, finally_process)
)
)
// --------------------------------------------------------------------------------------------
// promises / futures
future_1 = fetch(args)
future_2 = future.map(function(value) {fetch_using_response_1(value)})
future_3 = future.map(function(value) {fetch_using_response_2(value)})
finally_process(future_3.value)
// simplified anonymous function syntax
future_1 = fetch(args)
future_2 = future.map((value) => fetch_using_response_1(value))
future_3 = future.map((value) => fetch_using_response_2(value))
finally_process(future_3.value)
// further simplification: eliminate unnecessary anonymous functions
future_1 = fetch(args)
future_2 = future.map(fetch_using_response_1)
future_3 = future.map(fetch_using_response_2)
finally_process(future_3.value)
// --------------------------------------------------------------------------------------------
// async / await syntax
async function do_everything() {
tmp_1 = await fetch(args)
tmp_2 = await fetch_using_response_1(tmp_1)
tmp_3 = await fetch_using_response_2(tmp_2)
finally_process(tmp_3)
}