Need help with queue?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

jessetane
485 Stars 52 Forks MIT License 213 Commits 10 Opened issues

Description

Asynchronous function queue with adjustable concurrency

Services available

!
?

Need anything else?

Contributors list

# 159,492
HTML
Shell
Node.js
buffer
154 commits
# 26,708
Chrome
GraphQL
TypeScr...
headles...
8 commits
# 297,788
HTML
Shell
Android
Angular
8 commits
# 92,128
seriali...
node
node-js
React
3 commits
# 62,535
Arduino
armv6
Raspber...
C#
2 commits
# 618,882
TypeScr...
HTML
2 commits
# 657,332
TypeScr...
HTML
2 commits
# 5,610
webperf...
coffees...
express...
nextjs
1 commit
# 69,133
seleniu...
seleniu...
seleniu...
Angular
1 commit
# 426,185
HTML
Go
Shell
git-hoo...
1 commit
# 679,907
TypeScr...
HTML
1 commit
# 20,812
JavaScr...
ESLint
ecmascr...
helmet
1 commit
# 481,112
PHP
HTML
typo3-c...
Shell
1 commit
   ____  __  _____  __  _____
  / __ `/ / / / _ \/ / / / _ \
 / /_/ / /_/ /  __/ /_/ /  __/
 \__, /\__,_/\___/\__,_/\___/
   /_/

Asynchronous function queue with adjustable concurrency.

npm tests coverage

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()
.

Example

npm run example
``` javascript var queue = require('../')

var q = queue({ results: [] })

// add jobs using the familiar Array API q.push(function (cb) { const result = 'two' cb(null, result) })

q.push( function (cb) { const result = 'four' cb(null, result) }, function (cb) { const result = 'five' cb(null, result) } )

// jobs can accept a callback or return a promise q.push(function () { return new Promise(function (resolve, reject) { const result = 'one' resolve(result) }) })

q.unshift(function (cb) { const result = 'one' cb(null, result) })

q.splice(2, 0, function (cb) { const result = 'three' cb(null, result) })

// use the timeout feature to deal with jobs that // take too long or forget to execute a callback q.timeout = 100

q.on('timeout', function (next, job) { console.log('job timed out:', job.toString().replace(/\n/g, '')) next() })

q.push(function (cb) { setTimeout(function () { console.log('slow job finished') cb() }, 200) })

q.push(function (cb) { console.log('forgot to execute callback') })

// jobs can also override the queue's timeout // on a per-job basis function extraSlowJob (cb) { setTimeout(function () { console.log('extra slow job finished') cb() }, 400) }

extraSlowJob.timeout = 500 q.push(extraSlowJob)

// jobs can also opt-out of the timeout altogether function superSlowJob (cb) { setTimeout(function () { console.log('super slow job finished') cb() }, 1000) }

superSlowJob.timeout = null q.push(superSlowJob)

// get notified when jobs complete q.on('success', function (result, job) { console.log('job finished processing:', job.toString().replace(/\n/g, '')) console.log('The result is:', result) })

// begin processing, get notified on end / failure q.start(function (err) { if (err) throw err console.log('all done:', q.results) })

## Install
`npm install queue`

Note: You may need to install the events dependency if your environment does not have it by default (eg. browser, react-native).

Test

npm test npm run test-browser

API

var q = queue([opts])

Constructor. opts may contain inital values for:

  • q.concurrency
  • q.timeout
  • q.autostart
  • q.results

Instance methods

q.start([cb])

cb, if passed, will be called when the queue empties or when an error occurs.

q.stop()

Stops the queue. can be resumed with q.start().

q.end([err])

Stop and empty the queue immediately.

Instance methods mixed in from Array

Mozilla 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])

Properties

q.concurrency

Max number of jobs the queue should process concurrently, defaults to Infinity.

q.timeout

Milliseconds to wait for a job to execute its callback. This can be overridden by specifying a timeout property on a per-job basis.

q.autostart

Ensures the queue is always running if jobs are available. Useful in situations where you are using a queue only for concurrency control.

q.results

An array to set job callback arguments on.

q.length

Jobs pending + jobs to process (readonly).

Events

q.emit('start', job)

Immediately before a job begins to execute.

q.emit('success', result, job)

After a job executes its callback.

q.emit('error', err, job)

After a job passes an error to its callback.

q.emit('timeout', continue, job)

After q.timeout milliseconds have elapsed and a job has not executed its callback.

q.emit('end'[, err])

After all jobs have been processed

Releases

The latest stable release is published to npm. Abbreviated changelog below:

  • 6.0
    • Add start event before job begins (@joelgriffith)
    • Add timeout property on a job to override the queue's timeout (@joelgriffith)
  • 5.0
    • Updated TypeScript bindings (@Codex-)
  • 4.4
    • Add results feature
  • 4.3
    • Add promise support (@kwolfy)
  • 4.2
    • Unref timers on end
  • 4.1
    • Add autostart feature
  • 4.0
    • Change license to MIT
  • 3.1.x
    • Add .npmignore
  • 3.0.x
    • Change the default concurrency to Infinity
    • Allow q.start() to accept an optional callback executed on q.emit('end')
  • 2.x
    • Major api changes / not backwards compatible with 1.x
  • 1.x
    • Early prototype

License

Copyright © 2014 Jesse Tane <jesse.tane>

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See LICENSE for full details. </jesse.tane>

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.