1. Docs
  2. API

Complete surface

Small enough to hold in your head.

Three methods coordinate work. Four state properties expose and control the queue.

Methods

Append, advance, or clear.

Methods live on Queue.prototype. Every task is called with the queue instance as this.

method

add(...tasks)

Validates every argument, appends them in order, and starts the queue when eligible.

Accepts
Zero or more functions
Returns
The queue instance
Throws
TypeError if any value is not a function; nothing is appended from that call
method

next()

Runs and removes the first pending task unless stop is true. Calling it on an empty queue settles the queue as idle.

Accepts
No arguments
Returns
undefined
Throws
Rethrows a synchronous task error after resetting running
method

clear()

Replaces pending contents with a new empty array. The currently executing task continues.

Accepts
No arguments
Returns
The new empty array
Effect
Pending size becomes 0
constructor

new Queue()

Creates an idle queue with automatic start enabled and execution unblocked.

autoRun
true
stop
false
running
false
size
0

State

Observe two values. Control two values.

size and running are read-only getters. autoRun, stop, and contents are intentionally writable control surfaces.

Queue state
MemberDefaultWritableMeaning
contents[]YesThe actual pending function array. Replacement validates the complete array.
size0NoThe current pending item count; it excludes the active task.
runningfalseNoWhether a task currently owns execution.
autoRuntrueYesWhether an eligible add() starts the front task.
stopfalseYesWhether next() should hold pending work.
stop=false does not start work by itself.

After opening a stop gate, call next() to resume existing pending work. A later add() can also start an idle queue when autoRun is true.

See these members composed into connection gates and recovery flows.

Open patterns