Skip to content

Commit 603a0e8

Browse files
committed
feat(async): add async module
1 parent 0bb9953 commit 603a0e8

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/async.fnk

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
parallel = fn tasks:
3+
pipe [...tasks]:
4+
fold item, all=[]:
5+
[...all, await item]
6+
7+
8+
9+
sequential = fold item, all=[]:
10+
[...all, await item]
11+

src/async.test.fnk

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{describe, it, expect, to_equal, mock_fn} = import '@fink/jest'
2+
3+
{parallel, sequential} = import './async'
4+
5+
6+
run_task = fn name, handler:
7+
await handler '${name} start'
8+
await handler '${name} end'
9+
10+
11+
describe 'async', fn:
12+
it 'waits for tasks in parallel', fn:
13+
handler = mock_fn()
14+
15+
await pipe 'abc':
16+
map item: run_task item, handler
17+
parallel
18+
19+
expect
20+
handler.mock.calls
21+
to_equal list:
22+
['a start']
23+
['b start']
24+
['c start']
25+
['a end']
26+
['b end']
27+
['c end']
28+
29+
30+
31+
it 'pulls tasks sequentially', fn:
32+
handler = mock_fn()
33+
34+
await pipe 'abc':
35+
map item: run_task item, handler
36+
sequential
37+
38+
expect
39+
handler.mock.calls
40+
to_equal list:
41+
['a start']
42+
['a end']
43+
['b start']
44+
['b end']
45+
['c start']
46+
['c end']
47+

0 commit comments

Comments
 (0)