Constructor
new UnorderedWorkQueue(optionsopt)
Example
const sleep = ms => new Promise(r => setTimeout(r, ms));
const wq = new UnorderedWorkQueue({maxPending: 3});
await wq.put(sleep(1000));
await wq.put(sleep(1000));
await wq.put(sleep(1000));
await wq.put(sleep(1000)); // blocks for ~1 second waiting
for await (const _ of wq) {
// Handle results...
}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
UnorderedWorkQueueOptions |
<optional> |
Unordered work queue options. |
Classes
Methods
(async, generator) asCompleted() → {*}
An async generator that yields the results of completed tasks. Note that the
UnorderedWorkQueue
instance itself is also iterable and produces the same
results.
Example
const wq = new UnorderedWorkQueue(10);
wq.put(1);
wq.put(2);
for await (const x of wq.asCompleted()) {
console.log(x); // 1
// 2
}
// or...
wq.put(1);
wq.put(2);
for await (const x of wq) {
console.log(x); // 1
// 2
}
Yields:
Return values from completed jobs as soon as they are ready.
- Type
- *
fulfilled() → {Number}
Returns:
Jobs that are finished but have not be retrieved yet.
- Type
- Number
(async) get() → {*}
Get one result from the fulfilled queue.
Throws:
-
If
options.allowErrors
is unset and the job failed. - Type
- *
Returns:
The return value from a completed job.
- Type
- *
pending() → {Number}
Returns:
Jobs that have not finished yet or can not be retrieved yet.
- Type
- Number
(async) put(promise)
Add a new job to the work queue immediately if a spot is available. If
the pending queue or the fulfilled queues are full it will block.
Parameters:
Name | Type | Description |
---|---|---|
promise |
external:Promise | The awaitable to enqueue. |