UnorderedWorkQueue

UnorderedWorkQueue

A job control system that permits up to a fixed number of jobs (awaitables) to remain unfulfilled before blocking a producer from enqueueing any more work. In effect it provides a simple way to build a producer/consumer pattern with some regulation on the number of tasks the producer should enqueue based on fulfillment of the work. The results of each enqueue job are yielded in the order they finish, not the order they are enqueued.

Constructor

new UnorderedWorkQueue(optionsopt)

Source:
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

UnorderedWorkQueue

Methods

(async, generator) asCompleted() → {*}

Source:
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}

Source:
Returns:
Jobs that are finished but have not be retrieved yet.
Type
Number

(async) get() → {*}

Source:
See:
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}

Source:
Returns:
Jobs that have not finished yet or can not be retrieved yet.
Type
Number

(async) put(promise)

Source:
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.