Asynchronous function queue with adjustable concurrency
Asynchronous function queue with adjustable concurrency
____ __ _____ __ _____
/ __ `/ / / / _ \/ / / / _ \
/ /_/ / /_/ / __/ /_/ / __/
\__, /\__,_/\___/\__,_/\___/
/_/Asynchronous function queue with adjustable concurrency.
This module exports a class Queue that implements most of the Array API. Pass async functions (ones that accept a callback or return a promise) to an instance's additive array methods. Processing begins when you call q.start().
Do npm run example or npm run dev and open the example directory (and your console) to run the following program:
…npm install queue
yarn add queuenpm test
npm run dev // for testing in a browser, open test directory (and your console)const q = new Queue([opts])Constructor. opts may contain initial values for:
q.concurrencyq.timeoutq.autostartq.resultsq.start([cb])Explicitly starts processing jobs and provides feedback to the caller when the queue empties or an error occurs. If cb is not passed a promise will be returned.
q.stop()Stops the queue. can be resumed with q.start().
q.end([err])Stop and empty the queue immediately.
ArrayMozilla has docs on how these methods work here. Note that slice does not copy the queue.
q.push(element1, ..., elementN)q.unshift(element1, ..., elementN)q.splice(index , howMany[, element1[, ...[, elementN]]])q.pop()q.shift()q.slice(begin[, end])q.reverse()q.indexOf(searchElement[, fromIndex])q.lastIndexOf(searchElement[, fromIndex])q.concurrencyMax number of jobs the queue should process concurrently, defaults to Infinity.
q.timeoutMilliseconds to wait for a job to execute its callback. This can be overridden by specifying a timeout property on a per-job basis.
q.autostartEnsures the queue is always running if jobs are available. Useful in situations where you are using a queue only for concurrency control.
q.resultsAn array to set job callback arguments on.
q.lengthJobs pending + jobs to process (readonly).
q.dispatchEvent(new QueueEvent('start', { job }))Immediately before a job begins to execute.
q.dispatchEvent(new QueueEvent('success', { result: [...result], job }))After a job executes its callback.
q.dispatchEvent(new QueueEvent('error', { err, job }))After a job passes an error to its callback.
q.dispatchEvent(new QueueEvent('timeout', { next, job }))After q.timeout milliseconds have elapsed and a job has not executed its callback.
q.dispatchEvent(new QueueEvent('end', { err }))After all jobs have been processed
The latest stable release is published to npm. Abbreviated changelog below:
start event before job begins (@joelgriffith)timeout property on a job to override the queue's timeout (@joelgriffith)Infinityq.start() to accept an optional callback executed on q.emit('end')No open issues yet, or sync has not completed.