-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathasync.javascript.txt
131 lines (114 loc) · 9.96 KB
/
async.javascript.txt
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
┏━━━━━━━━━━━┓
┃ ASYNC ┃
┗━━━━━━━━━━━┛
VERSION ==> #0.9.0
#Node module or web script
#All functions are async, which is the main feature.
┌───────────────────────────┐
│ ASYNC ARRAY FUNCTIONS │
└───────────────────────────┘
ASYNC.each(ARR, FUNC(VAL, FUNC2(ERROR)) #Like ARR.forEach(FUNC(VAL)) but async in parallel. Uses callbacks FUNC2().
[, FUNC3(ERROR)]) #FUNC3() is called after last FUNC(), or at first FUNC2() ERROR (which stops
#everything).
ASYNC.map(ARR, FUNC(VAL, FUNC2(ERROR, VAL2)),
FUNC3(ERROR, ARR2)) #Same for ARR.map(FUNC(VAL)->VAL2)->ARR2
ASYNC.reduce[Right](ARR, VAL5, FUNC(VAL, VAL4,
FUNC2(ERROR, VAL2)), FUNC3(ERROR, VAL3)) #Same for ARR.reduce[Right](FUNC(VAL4, VAL)->VAL2, VAL5)->VAL3
ASYNC.filter|reject(ARR, FUNC(VAL, FUNC2(BOOL)),
FUNC3(ARR2)) #Same for ARR.filter|reject(FUNC(VAL)->BOOL)->ARR2
ASYNC.some|every(ARR, FUNC(VAL, FUNC2(BOOL)),
FUNC3(BOOL2)) #Same for ARR.some|every(FUNC(VAL)->BOOL)->BOOL2
ASYNC.sortBy(ARR, FUNC(VAL, FUNC2(ERROR, UINT)),
FUNC3(ERROR, ARR2)) #ARR2 is ARR sorted according to each VAL weight UINT
ASYNC.detect(ARR, FUNC(VAL, FUNC2(BOOL)),
FUNC3(VAL)) #Returns first VAL that returns true in ARR (or undefined of none did)
ASYNC.each|mapLimit(ARR, UINT,...) #Same as ASYNC.FUNC(ARR,...) but execute at most UINT iterations at once.
ASYNC.each|map|filter|reject|detectSeries(ARR,...)#Like ASYNC.eachLimit(ARR, 1, ...), so executes serially instead of in
#parallel.
┌──────────────────┐
│ CONTROL FLOW │
└──────────────────┘
ASYNC.series|parallel #Executes all FUNC() serially or in parallel, and returns VAL concatenated
(FUNC(FUNC2(ERROR, VAL))_ARR|OBJ, #as VAL_ARR|OBJ
FUNC3(ERROR, VAL_ARR|OBJ)) #If ERROR, stops and fires FUNC3() right away
ASYNC.parallelLimit(..., UINT, FUNC3) #
ASYNC.waterfall
(FUNC([VAL..., ]FUNC2(ERROR[, VAL...]))_ARR, #Same as ASYNC.series() but each function pass arguments to the next one,
FUNC3(ERROR, VAL...)) #and ultimately to FUNC3()
ASYNC.seq(...)->FUNC4([VAL...,]) #FUNC4() fires ASYNC.waterfall(...), with:
# - VAL... as initial arguments
# - FUNC... instead of FUNC_ARR
ASYNC.compose(...) #Same as ASYNC.seq(...) but:
->FUNC4([VAL...,]FUNC3(ERROR, VAL...)) # - FUNC... are in reverse order
# - final callback FUNC3() is provided by FUNC4()
ASYNC.applyEach[Series] #Like ASYNC.parallel|series(FUNC_ARR, FUNC3) except:
(FUNC([VAL...,]FUNC2(ERROR))_ARR # - FUNC2() don't pass result to FUNC3()
[[, VAL...], FUNC3(ERROR)]) # - FUNC2() can take arguments VAL... provided by applyEach[Series]
# - if no FUNC3(ERROR), returns FUNC4([VAL..., ]FUNC3(ERROR)) that
# finally fires everything
ASYNC.whilst(FUNC4()->BOOL, FUNC(FUNC2(ERROR)), #Same as while ( FUNC4() ) FUNC(), but async.
FUNC3(ERROR)) #FUNC3() is called at end, or if error
ASYNC.dowhilst|until|dountil(...) #Same for do while, until, do until
ASYNC.forever(...) #Same as ASYNC.whilst(FUNC()->true, ...)
┌────────────────┐
│ ITERATIONS │
└────────────────┘
ASYNC.retry(UINT, FUNC(FUNC2(ERROR, VAL)), #Fires FUNC() UINT times until ERROR == null.
FUNC3(ERROR, VAL)) #Then fires FUNC3(...) with last FUNC2(...) arguments.
ASYNC.times[Series](UINT, #Fires FUNC() UINT times serially or in parallel, and get all VAL returned
FUNC(UINT2,FUNC2(ERROR,VAL)), FUNC3(EROR,VAL_ARR))#as VAL_ARR.
#UINT2 is current iteration number (starting at 0)
ASYNC.iterator(FUNC(...)_ARR) #Returns ITERATOR where each ITERATOR[.next](...):
# - unless "next", fires current FUNC(...)
# - returns ITERATOR2 for next FUNC, or null if none.
┌────────────┐
│ QUEUES │
└────────────┘
ASYNC.queue(FUNC(VAL, FUNC2([VAL2...])), UINT) #Returns QUEUE with methods:
# - push(VAL[, FUNC3([VAL2...])]):
# - fires FUNC(VAL).
# - Waits for previous push() to async complete.
# - at most UINT items run at once
# - if VAL is ARR, do several push(VAL)
# - unshift(...): same but put on the front of the queue, so will be first
# to be proceeded next
# - length|running(): number of items waiting|running
# - started BOOL: if length() > 0
# - idle()->BOOL: if length|running() > 0
# - concurrency: same as UINT. Can be changed
# - saturated(): event handler when running() == UINT
# - empty(): event handler when length() == 0
# - drain(): event handler when length() and running() == 0
# - pause|resume()
# - paused BOOL
# - kill(): removes waiting items
ASYNC.priorityQueue #Same except items are processed by priority UINT2, not by order of push:
(FUNC(VAL, FUNC2([VAL2...])), UINT) # - use push(VAL, UINT2[, FUNC3([VAL2...])])
# - no unshift()
#Lower UINT2 -> faster processing.
ASYNC.cargo(...) #Same as ASYNC.queue(...) except:
# - waits to be saturated() to fire callbacks
# - fire FUNC(VAL_ARR) once for several item, concatenating them as VAL_ARR
# - concurrency -> payload
# - drain() -> drained BOOL
# - no unshift(), pause(), resume(), paused, kill()
ASYNC.auto(OBJ, FUNC(ERROR, OBJ2)) #Similar to ASYNC.parallel, but with dependencies and message passing.
#OBJ is:
# - KEY: FUNC2(FUNC3(ERROR, VAL), OBJ2)
# - fires FUNC2() which fills OBJ2.KEY = VAL
# - KEY2: [ KEY..., FUNC2(...) ]:
# - only fires FUNC2(...) once KEY... have completed
# - if ERROR, stops and pass control to FUNC()
┌───────────────┐
│ UTILITIES │
└───────────────┘
ASYNC.nextTick(FUNC()) #In Node.js, process.nextTick(), in browser, WINDOW.setImmediate(FUNC())
ASYNC.log|dir #Calls FUNC() with VAL..., then:
(FUNC([VAL...,] FUNC2(ERROR[,VAL2...]))[,VAL...]) # - fires console.log|dir(VAL2) for each VAL2...
# - if ERROR, fires console.error(ERROR) instead
#Async. version of console.log|error
ASYNC.memoize(FUNC([VAL..., ]FUNC2(VAL2...)) #Same as __.memoize(FUNC([VAL...])->VAL2[, FUNC3]) but async.:
[, FUNC3(VAL)]) -> FUNC4 # - FUNC3() VAL is first of VAL... (def: return it as is)
# - can use FUNC4.unmemoized() to refer to original uncached FUNC()
# - uses FUNC4.memo instead of FUNC4.cache
ASYNC.unmemoize(FUNC4) #