1
- # async
1
+ # asyncutil
2
2
3
- [ ![ jsr] ( https://img.shields.io/jsr/v/%40lambdalisue/async?logo=javascript&logoColor=white )] ( https://jsr.io/@lambdalisue/async )
4
- [ ![ denoland] ( https://img.shields.io/github/v/release/lambdalisue/deno-async?logo=deno&label=denoland )] ( https://github.com/lambdalisue/deno-async/releases )
5
- [ ![ deno doc] ( https://doc.deno.land/badge.svg )] ( https://doc.deno.land/https/deno.land/x/async/mod.ts )
6
- [ ![ Test] ( https://github.com/lambdalisue/deno-async/workflows/Test/badge.svg )] ( https://github.com/lambdalisue/deno-async/actions?query=workflow%3ATest )
3
+ [ ![ Test] ( https://github.com/jsr-core/asyncutil/actions/workflows/test.yml/badge.svg )] ( https://github.com/jsr-core/asyncutil/actions/workflows/test.yml )
7
4
8
- Asynchronous primitive modules for [ Deno] [ deno ] .
9
-
10
- [ python's asyncio ] : https://docs.python.org/3/library/asyncio.html
11
- [ deno ] : https://deno.land/
5
+ Asynchronous primitive utility pack.
12
6
13
7
## Usage
14
8
15
- ### Barrier
9
+ ### AsyncValue
16
10
17
- ` Barrier ` is a synchronization primitive that allows multiple tasks to wait
18
- until all of them have reached a certain point of execution before continuing .
11
+ ` AsyncValue ` is a class that wraps a value and allows it to be set
12
+ asynchronously .
19
13
20
14
``` ts
21
- import { Barrier } from " https://deno.land/x/async@$MODULE_VERSION/barrier.ts" ;
22
-
23
- const barrier = new Barrier (3 );
24
-
25
- async function worker(id : number ) {
26
- console .log (` worker ${id } is waiting ` );
27
- await barrier .wait ();
28
- console .log (` worker ${id } is done ` );
29
- }
15
+ import { assertEquals } from " @std/assert" ;
16
+ import { AsyncValue } from " @core/asyncutil/async-value" ;
30
17
31
- worker (1 );
32
- worker (2 );
33
- worker (3 );
18
+ const v = new AsyncValue (0 );
19
+ assertEquals (await v .get (), 0 );
20
+ await v .set (1 );
21
+ assertEquals (await v .get (), 1 );
34
22
```
35
23
36
- ### WaitGroup
24
+ ### Barrier
37
25
38
- ` WaitGroup ` is a synchronization primitive that enables promises to coordinate
39
- and synchronize their execution. It is particularly useful in scenarios where a
40
- specific number of tasks must complete before the program can proceed.
26
+ ` Barrier ` is a synchronization primitive that allows multiple tasks to wait
27
+ until all of them have reached a certain point of execution before continuing.
41
28
42
29
``` ts
43
- import {
delay }
from " https://deno.land/[email protected] /async/delay.ts" ;
44
- import { WaitGroup } from " https://deno.land/x/async@$MODULE_VERSION/wait_group.ts" ;
30
+ import { Barrier } from " @core/asyncutil/barrier" ;
45
31
46
- const wg = new WaitGroup ( );
32
+ const barrier = new Barrier ( 3 );
47
33
48
34
async function worker(id : number ) {
49
- wg .add (1 );
50
35
console .log (` worker ${id } is waiting ` );
51
- await delay ( 100 );
36
+ await barrier . wait ( );
52
37
console .log (` worker ${id } is done ` );
53
- wg .done ();
54
38
}
55
39
56
40
worker (1 );
57
41
worker (2 );
58
42
worker (3 );
59
- await wg .wait ();
60
43
```
61
44
62
45
### Lock/RwLock
@@ -65,8 +48,8 @@ await wg.wait();
65
48
shared value.
66
49
67
50
``` ts
68
- import { AsyncValue } from " https://deno.land/x/ async@$MODULE_VERSION/testutil.ts " ;
69
- import { Lock } from " https://deno.land/x/async@$MODULE_VERSION/ lock.ts " ;
51
+ import { AsyncValue } from " @core/asyncutil/ async-value " ;
52
+ import { Lock } from " @core/asyncutil/ lock" ;
70
53
71
54
// Critical section
72
55
const count = new Lock (new AsyncValue (0 ));
@@ -82,8 +65,8 @@ as long as there are no writers holding the lock. Writers block all other
82
65
readers and writers until the write operation completes.
83
66
84
67
``` ts
85
- import { AsyncValue } from " https://deno.land/x/ async@$MODULE_VERSION/testutil.ts " ;
86
- import { RwLock } from " https://deno.land/x/async@$MODULE_VERSION/rw_lock.ts " ;
68
+ import { AsyncValue } from " @core/asyncutil/ async-value " ;
69
+ import { RwLock } from " @core/asyncutil/rw-lock " ;
87
70
88
71
const count = new RwLock (new AsyncValue (0 ));
89
72
@@ -117,8 +100,8 @@ This is a low-level primitive. Use `Lock` instead of `Mutex` if you need to
117
100
access a shared value concurrently.
118
101
119
102
``` ts
120
- import { AsyncValue } from " https://deno.land/x/ async@$MODULE_VERSION/testutil.ts " ;
121
- import { Mutex } from " https://deno.land/x/async@$MODULE_VERSION/ mutex.ts " ;
103
+ import { AsyncValue } from " @core/asyncutil/ async-value " ;
104
+ import { Mutex } from " @core/asyncutil/ mutex" ;
122
105
123
106
const count = new AsyncValue (0 );
124
107
@@ -127,13 +110,12 @@ async function doSomething() {
127
110
await count .set (v + 1 );
128
111
}
129
112
130
- // Critical section
131
113
const mu = new Mutex ();
132
- await mu .acquire ();
133
- try {
114
+
115
+ // Critical section
116
+ {
117
+ using _lock = await mu .acquire ();
134
118
await doSomething ();
135
- } finally {
136
- mu .release ();
137
119
}
138
120
```
139
121
@@ -143,9 +125,9 @@ try {
143
125
notification.
144
126
145
127
``` ts
146
- import {
assertEquals }
from " https://deno.land/[email protected] /assert/mod.ts " ;
147
- import { promiseState } from " https://deno.land/x/async@$MODULE_VERSION/ state.ts " ;
148
- import { Notify } from " https://deno.land/x/async@$MODULE_VERSION/ notify.ts " ;
128
+ import { assertEquals } from " @std /assert" ;
129
+ import { promiseState } from " @core/asyncutil/promise- state" ;
130
+ import { Notify } from " @core/asyncutil/ notify" ;
149
131
150
132
const notify = new Notify ();
151
133
const waiter1 = notify .notified ();
@@ -158,14 +140,32 @@ assertEquals(await promiseState(waiter1), "fulfilled");
158
140
assertEquals (await promiseState (waiter2 ), " fulfilled" );
159
141
```
160
142
143
+ ### promiseState
144
+
145
+ ` promiseState ` is used to determine the state of the promise. Mainly for testing
146
+ purpose.
147
+
148
+ ``` typescript
149
+ import { promiseState } from " @core/asyncutil/promise-state" ;
150
+
151
+ const p1 = Promise .resolve (" Resolved promise" );
152
+ console .log (await promiseState (p1 )); // fulfilled
153
+
154
+ const p2 = Promise .reject (" Rejected promise" ).catch (() => undefined );
155
+ console .log (await promiseState (p2 )); // rejected
156
+
157
+ const p3 = new Promise (() => undefined );
158
+ console .log (await promiseState (p3 )); // pending
159
+ ```
160
+
161
161
### Queue/Stack
162
162
163
163
` Queue ` is a queue implementation that allows for adding and removing elements,
164
164
with optional waiting when popping elements from an empty queue.
165
165
166
166
``` ts
167
- import {
assertEquals }
from " https://deno.land/[email protected] /assert/mod.ts " ;
168
- import { Queue } from " https://deno.land/x/async@$MODULE_VERSION/ queue.ts " ;
167
+ import { assertEquals } from " @std /assert" ;
168
+ import { Queue } from " @core/asyncutil/ queue" ;
169
169
170
170
const queue = new Queue <number >();
171
171
queue .push (1 );
@@ -180,8 +180,8 @@ assertEquals(await queue.pop(), 3);
180
180
with optional waiting when popping elements from an empty stack.
181
181
182
182
``` ts
183
- import {
assertEquals }
from " https://deno.land/[email protected] /assert/mod.ts " ;
184
- import { Stack } from " https://deno.land/x/async@$MODULE_VERSION/ stack.ts " ;
183
+ import { assertEquals } from " @std /assert" ;
184
+ import { Stack } from " @core/asyncutil/ stack" ;
185
185
186
186
const stack = new Stack <number >();
187
187
stack .push (1 );
@@ -198,7 +198,7 @@ A semaphore that allows a limited number of concurrent executions of an
198
198
operation.
199
199
200
200
``` ts
201
- import { Semaphore } from " https://deno.land/x/async@$MODULE_VERSION/ semaphore.ts " ;
201
+ import { Semaphore } from " @core/asyncutil/ semaphore" ;
202
202
203
203
const sem = new Semaphore (5 );
204
204
const worker = () => {
@@ -209,37 +209,30 @@ const worker = () => {
209
209
await Promise .all ([... Array (10 )].map (() => worker ()));
210
210
```
211
211
212
- ### promiseState
213
-
214
- ` promiseState ` is used to determine the state of the promise. Mainly for testing
215
- purpose.
216
-
217
- ``` typescript
218
- import { promiseState } from " https://deno.land/x/async@$MODULE_VERSION/mod.ts" ;
219
-
220
- const p1 = Promise .resolve (" Resolved promise" );
221
- console .log (await promiseState (p1 )); // fulfilled
222
-
223
- const p2 = Promise .reject (" Rejected promise" ).catch (() => undefined );
224
- console .log (await promiseState (p2 )); // rejected
212
+ ### WaitGroup
225
213
226
- const p3 = new Promise (() => undefined );
227
- console . log ( await promiseState ( p3 )); // pending
228
- ```
214
+ ` WaitGroup ` is a synchronization primitive that enables promises to coordinate
215
+ and synchronize their execution. It is particularly useful in scenarios where a
216
+ specific number of tasks must complete before the program can proceed.
229
217
230
- ### AsyncValue
218
+ ``` ts
219
+ import { delay } from " @std/async/delay" ;
220
+ import { WaitGroup } from " @core/asyncutil/wait-group" ;
231
221
232
- ` AsyncValue ` is a class that wraps a value and allows it to be set
233
- asynchronously.
222
+ const wg = new WaitGroup ();
234
223
235
- ``` ts
236
- import {
assertEquals }
from " https://deno.land/[email protected] /assert/mod.ts" ;
237
- import { AsyncValue } from " https://deno.land/x/async@$MODULE_VERSION/testutil.ts" ;
224
+ async function worker(id : number ) {
225
+ wg .add (1 );
226
+ console .log (` worker ${id } is waiting ` );
227
+ await delay (100 );
228
+ console .log (` worker ${id } is done ` );
229
+ wg .done ();
230
+ }
238
231
239
- const v = new AsyncValue ( 0 );
240
- assertEquals ( await v . get (), 0 );
241
- await v . set ( 1 );
242
- assertEquals ( await v . get (), 1 );
232
+ worker ( 1 );
233
+ worker ( 2 );
234
+ worker ( 3 );
235
+ await wg . wait ( );
243
236
```
244
237
245
238
## License
0 commit comments