-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.js
executable file
·151 lines (122 loc) · 3.59 KB
/
callbacks.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// const final = (callback, done=0, expect=0, exit=false, data=[]) =>
// c =>
// ( c = expect++,
// (err, ...res) =>
// exit ||
// err
// ?
// ( exit = true,
// callback(err, c) )
// : ( data[c] = res,
// ++done >= expect
// ?
// ( exit = true,
// callback(null, data) )
// : null) )
// const stringify = data =>
// data.map ? data.map(x => JSON.stringify(x)).join(', ') : data
// const taskhandle = final((err, data) =>
// (console.log('We are done!'), err && console.log('err! ', data),
// data && console.log(`results by task index: ${stringify(data)}`)))
// const run = (x, join=taskhandle()) =>
// setTimeout(() => console.log(`finishing task ${x}`) || join(null, x), 50*x*Math.random())
// let x = -1
// while (++x<100) run(x)
// // try again, less statements this time
// (final, taskhandle, stringify)
// const final = (callback, done=0, expect=0, exit=false, data=[]) =>
// c =>
// ( c = expect++,
// (err, ...res) =>
// exit ||
// err
// ?
// ( exit = true,
// callback(err, c) )
// : ( data[c] = res,
// ++done >= expect
// ?
// ( exit = true,
// callback(null, data) )
// : null) )
// const stringify = data =>
// data.map ? data.map(x => JSON.stringify(x)).join(', ') : data
// const taskhandle = final((err, data) =>
// (console.log('We are done!'), err && console.log('err! ', data),
// data && console.log(`results by task index: ${stringify(data)}`)))
// const run = (_, idx, __, join=taskhandle()) => (
// setTimeout(() => console.log(`finishing task ${idx}`) || join(null, idx), variation*idx*Math.random())
// )
// //#require
// (($stringify, $final, variation=50, count=100) => (
// //#require
// (($taskhandle) => (
// [...new Array(count)].map(
// (_, idx, __, $join=$taskhandle()) => (
// setTimeout(() => (
// console.log(`finishing task ${idx}`) || $join(null, idx)
// ), variation*idx*Math.random())
// )
// )
// ))(
// //#deps
// //$taskhandle
// $final((err, data) => (
// console.log('We are done!'), err && console.log('err! ', data),
// data && console.log(`results by task index: ${$stringify(data)}`)
// ))
// )
// ))(
// //#deps
// //$stringify
// (data) => (
// data.map ? data.map(x => JSON.stringify(x)).join(', ') : data
// ),
// //$final
// (callback, done=0, expect=0, exit=false, data=[]) => (
// //$join
// c => (
// c = expect++,
// (err, ...res) => (
// exit ||
// (err || (data[c] = res, ++done >= expect)) &&
// (exit = true, callback(err, err ? c : data))
// )
// )
// )
// )
//#require
(($stringify, $final, variation=50, count=100) =>
//#require
void (($taskhandle) =>
void [...new Array(count)].map(
(_, idx, __, $join=$taskhandle()) =>
void setTimeout(() =>
void console.log(`finishing task ${idx}`) || void $join(null, idx),
variation*idx*Math.random()))
)(
//#deps
// (callback) -> $taskhandle
$final((err, data) =>
void console.log('We are done!') ||
err && void console.log('err! ', data) ||
data && void console.log(`results by task index: ${$stringify(data)}`))
)
)(
//#deps
//$stringify
data =>
data.map ? data.map(x => JSON.stringify(x)).join(', ') : data,
//$final
(callback, done=0, expect=0, exit=false, data=[]) =>
//$taskhandle
c => (
c = expect++,
//$join
(err, ...res) =>
exit ||
(err || (data[c] = res, ++done >= expect)) &&
void (exit = true, callback(err, err ? c : data))
)
)
// ... what was that again?