You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constqueue=newAsyncQueue<number>() // generic can optionally be used
35
+
constpool=newAsyncPool<number>() // generic can optionally be used
36
36
.withConcurrency(10)
37
37
.withRetries(3); // default number of retries for each task unless specified at task level
38
38
39
-
//enqueue many tasks
39
+
//add many tasks
40
40
for (let i =0; i<100; i++) {
41
-
queue.enqueue({
41
+
pool.add({
42
42
task: async () =>2*i,
43
-
maxRetries: 3, // optional, will override the queue's default when set
43
+
maxRetries: 3, // optional, will override the pool's default when set
44
44
});
45
45
}
46
46
47
47
// consume results as a stream, without building a large array of results
48
48
let sum =0;
49
-
forawait (const res ofqueue.results()) {
49
+
forawait (const res ofpool.results()) {
50
50
sum+=res;
51
51
}
52
52
@@ -63,51 +63,51 @@ const task = async () => {
63
63
console.log("hello, world");
64
64
};
65
65
66
-
// create queue and add some tasks
67
-
constqueue=newAsyncQueue()
66
+
// create pool and add some tasks
67
+
constpool=newAsyncPool()
68
68
.withConcurrency(10)
69
69
.withRetries(3)
70
-
.enqueue({ task })
71
-
.enqueue({ task })
72
-
.enqueue({ task });
70
+
.add({ task })
71
+
.add({ task })
72
+
.add({ task });
73
73
74
74
// wait until all tasks are processed
75
-
awaitqueue.waitForTermination();
75
+
awaitpool.waitForTermination();
76
76
```
77
77
78
78
### Promise-based consumption
79
79
80
80
Similar to `Promise.all`, but with controlled concurrency and builtin retries
81
81
82
82
```typescript
83
-
constqueue=newAsyncQueue();
83
+
constpool=newAsyncPool();
84
84
85
-
queue.enqueue({ task: async () =>1 });
86
-
queue.enqueue({ task: async () =>true });
87
-
queue.enqueue({ task: async () =>"hello" });
85
+
pool.add({ task: async () =>1 });
86
+
pool.add({ task: async () =>true });
87
+
pool.add({ task: async () =>"hello" });
88
88
89
-
const results =awaitqueue.all();
89
+
const results =awaitpool.all();
90
90
console.log(results); // [1, true, "hello"], order not guaranteed (especially if retries happened)
91
91
```
92
92
93
93
### Using generic typings
94
94
95
-
You can specify a generic type for the `AsyncQueue` to enforce type safety on the results of the tasks. If you don't specify a type, it will default to `unknown`, allowing any type of result.
95
+
You can specify a generic type for the `AsyncPool` to enforce type safety on the results of the tasks. If you don't specify a type, it will default to `unknown`, allowing any type of result.
96
96
97
97
```typescript
98
-
consttypedQueue=newAsyncQueue<string>();
98
+
consttypedPool=newAsyncPool<string>();
99
99
100
-
typedQueue.enqueue({ task: async () =>"hello" }); // OK
101
-
typedQueue.enqueue({ task: async () =>1 }); // ❌ Error: Type 'Promise<number>' is not assignable to type 'Promise<string>'.
100
+
typedPool.add({ task: async () =>"hello" }); // OK
101
+
typedPool.add({ task: async () =>1 }); // ❌ Error: Type 'Promise<number>' is not assignable to type 'Promise<string>'.
102
102
103
-
//typedQueue.all() will return a Promise<string[]>, typedQueue.results() is an AsyncGenerator<string>
103
+
//typedPool.all() will return a Promise<string[]>, typedPool.results() is an AsyncGenerator<string>
104
104
105
-
constrelaxedQueue=newAsyncQueue();
105
+
constrelaxedPool=newAsyncPool();
106
106
107
-
relaxedQueue.enqueue({ task: async () =>"hello" }); // OK
108
-
relaxedQueue.enqueue({ task: async () =>1 }); // OK
107
+
relaxedPool.add({ task: async () =>"hello" }); // OK
108
+
relaxedPool.add({ task: async () =>1 }); // OK
109
109
110
-
//relaxedQueue.all() will return a Promise<unknown[]>, relaxedQueue.results() is an AsyncGenerator<unknown>
110
+
//relaxedPool.all() will return a Promise<unknown[]>, relaxedPool.results() is an AsyncGenerator<unknown>
constqueue=newAsyncQueue<number>() // generic can optionally be used
38
+
constpool=newAsyncPool<number>() // generic can optionally be used
25
39
.withConcurrency(10)
26
40
.withRetries(3); // default number of retries for each task unless specified at task level
27
41
28
-
//enqueue many tasks
42
+
//add many tasks
29
43
for (let i =0; i<100; i++) {
30
-
queue.enqueue({
44
+
pool.add({
31
45
task: async () =>2*i,
32
-
maxRetries: 3, // optional, will override the queue's default when set
46
+
maxRetries: 3, // optional, will override the pool's default when set
33
47
});
34
48
}
35
49
36
50
// consume results as a stream, without building a large array of results
37
51
let sum =0;
38
-
forawait (const res ofqueue.results()) {
52
+
forawait (const res ofpool.results()) {
39
53
sum+=res;
40
54
}
41
55
@@ -52,51 +66,51 @@ const task = async () => {
52
66
console.log("hello, world");
53
67
};
54
68
55
-
// create queue and add some tasks
56
-
constqueue=newAsyncQueue()
69
+
// create pool and add some tasks
70
+
constpool=newAsyncPool()
57
71
.withConcurrency(10)
58
72
.withRetries(3)
59
-
.enqueue({ task })
60
-
.enqueue({ task })
61
-
.enqueue({ task });
73
+
.add({ task })
74
+
.add({ task })
75
+
.add({ task });
62
76
63
77
// wait until all tasks are processed
64
-
awaitqueue.waitForTermination();
78
+
awaitpool.waitForTermination();
65
79
```
66
80
67
81
### Promise-based consumption
68
82
69
83
Similar to `Promise.all`, but with controlled concurrency and builtin retries
70
84
71
85
```typescript
72
-
constqueue=newAsyncQueue();
86
+
constpool=newAsyncPool();
73
87
74
-
queue.enqueue({ task: async () =>1 });
75
-
queue.enqueue({ task: async () =>true });
76
-
queue.enqueue({ task: async () =>"hello" });
88
+
pool.add({ task: async () =>1 });
89
+
pool.add({ task: async () =>true });
90
+
pool.add({ task: async () =>"hello" });
77
91
78
-
const results =awaitqueue.all();
92
+
const results =awaitpool.all();
79
93
console.log(results); // [1, true, "hello"], order not guaranteed (especially if retries happened)
80
94
```
81
95
82
96
### Using generic typings
83
97
84
-
You can specify a generic type for the `AsyncQueue` to enforce type safety on the results of the tasks. If you don't specify a type, it will default to `unknown`, allowing any type of result.
98
+
You can specify a generic type for the `AsyncPool` to enforce type safety on the results of the tasks. If you don't specify a type, it will default to `unknown`, allowing any type of result.
85
99
86
100
```typescript
87
-
consttypedQueue=newAsyncQueue<string>();
101
+
consttypedPool=newAsyncPool<string>();
88
102
89
-
typedQueue.enqueue({ task: async () =>"hello" }); // OK
90
-
typedQueue.enqueue({ task: async () =>1 }); // ❌ Error: Type 'Promise<number>' is not assignable to type 'Promise<string>'.
103
+
typedPool.add({ task: async () =>"hello" }); // OK
104
+
typedPool.add({ task: async () =>1 }); // ❌ Error: Type 'Promise<number>' is not assignable to type 'Promise<string>'.
91
105
92
-
//typedQueue.all() will return a Promise<string[]>, typedQueue.results() is an AsyncGenerator<string>
106
+
//typedPool.all() will return a Promise<string[]>, typedPool.results() is an AsyncGenerator<string>
93
107
94
-
constrelaxedQueue=newAsyncQueue();
108
+
constrelaxedPool=newAsyncPool();
95
109
96
-
relaxedQueue.enqueue({ task: async () =>"hello" }); // OK
97
-
relaxedQueue.enqueue({ task: async () =>1 }); // OK
110
+
relaxedPool.add({ task: async () =>"hello" }); // OK
111
+
relaxedPool.add({ task: async () =>1 }); // OK
98
112
99
-
//relaxedQueue.all() will return a Promise<unknown[]>, relaxedQueue.results() is an AsyncGenerator<unknown>
113
+
//relaxedPool.all() will return a Promise<unknown[]>, relaxedPool.results() is an AsyncGenerator<unknown>
0 commit comments