Comparing version 4.2.1 to 4.3.1
17
index.js
@@ -63,5 +63,7 @@ var inherits = require('inherits') | ||
Object.defineProperty(Queue.prototype, 'length', { get: function () { | ||
return this.pending + this.jobs.length | ||
}}) | ||
Object.defineProperty(Queue.prototype, 'length', { | ||
get: function () { | ||
return this.pending + this.jobs.length | ||
} | ||
}) | ||
@@ -131,3 +133,10 @@ Queue.prototype.start = function (cb) { | ||
this.pending++ | ||
job(next) | ||
var promise = job(next) | ||
if (promise && promise.then && typeof promise.then === 'function') { | ||
promise.then(function (result) { | ||
next(null, result) | ||
}).catch(function (err) { | ||
next(err || true) | ||
}) | ||
} | ||
@@ -134,0 +143,0 @@ if (this.jobs.length > 0) { |
{ | ||
"name": "queue", | ||
"version": "4.2.1", | ||
"version": "4.3.1", | ||
"description": "asynchronous function queue with adjustable concurrency", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
185
readme.md
@@ -14,91 +14,90 @@ ``` | ||
## Why | ||
[Async](https://github.com/caolan/async) is a big library offering various approaches to dealing with asynchrony; `queue` is a small library offering a single, flexible abstraction. | ||
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()`. | ||
## How | ||
This module exports a class `Queue` that implements most of the `Array` API. Pass async functions (ones that accept a callback) to an instance's additive array methods. Processing begins when you call `q.start()`. | ||
## Install | ||
`npm install queue` | ||
## Test | ||
`npm test` | ||
`npm run test-browser` | ||
## Example | ||
`npm run example` | ||
``` javascript | ||
var queue = require('queue'); | ||
var queue = require('../') | ||
var q = queue(); | ||
var results = []; | ||
var q = queue() | ||
var results = [] | ||
// add jobs using the Array API | ||
// add jobs using the familiar Array API | ||
q.push(function (cb) { | ||
results.push('two') | ||
cb() | ||
}) | ||
q.push(function(cb) { | ||
results.push('two'); | ||
cb(); | ||
}); | ||
q.push( | ||
function(cb) { | ||
results.push('four'); | ||
cb(); | ||
function (cb) { | ||
results.push('four') | ||
cb() | ||
}, | ||
function(cb) { | ||
results.push('five'); | ||
cb(); | ||
function (cb) { | ||
results.push('five') | ||
cb() | ||
} | ||
); | ||
) | ||
q.unshift(function(cb) { | ||
results.push('one'); | ||
cb(); | ||
}); | ||
// jobs can accept a callback or return a promise | ||
q.push(function () { | ||
return new Promise(function (resolve, reject) { | ||
results.push('one') | ||
resolve() | ||
}) | ||
}) | ||
q.splice(2, 0, function(cb) { | ||
results.push('three'); | ||
cb(); | ||
}); | ||
q.unshift(function (cb) { | ||
results.push('one') | ||
cb() | ||
}) | ||
q.splice(2, 0, function (cb) { | ||
results.push('three') | ||
cb() | ||
}) | ||
// use the timeout feature to deal with jobs that | ||
// take too long or forget to execute a callback | ||
q.timeout = 100 | ||
q.timeout = 100; | ||
q.on('timeout', function (next, job) { | ||
console.log('job timed out:', job.toString().replace(/\n/g, '')) | ||
next() | ||
}) | ||
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) { | ||
setTimeout(function() { | ||
console.log('slow job finished'); | ||
cb(); | ||
}, 200); | ||
}); | ||
q.push(function (cb) { | ||
console.log('forgot to execute callback') | ||
}) | ||
q.push(function(cb) { | ||
console.log('forgot to execute callback'); | ||
}); | ||
// get notified when jobs complete | ||
q.on('success', function (result, job) { | ||
console.log('job finished processing:', job.toString().replace(/\n/g, '')) | ||
}) | ||
q.on('success', function(result, job) { | ||
console.log('job finished processing:', job.toString().replace(/\n/g, '')); | ||
}); | ||
// begin processing, get notified on end / failure | ||
q.start(function(err) { | ||
console.log('all done:', results); | ||
}); | ||
q.start(function (err) { | ||
if (err) throw err | ||
console.log('all done:', results) | ||
}) | ||
``` | ||
## Require | ||
#### `var queue = require('queue')` | ||
## Install | ||
`npm install queue` | ||
## Constructor | ||
#### `var q = queue([opts])` | ||
Where `opts` may contain inital values for: | ||
## Test | ||
`npm test` | ||
`npm run test-browser` | ||
## API | ||
### `var q = queue([opts])` | ||
Constructor. `opts` may contain inital values for: | ||
* `q.concurrency` | ||
@@ -109,9 +108,9 @@ * `q.timeout` | ||
## Instance methods | ||
#### `q.start([cb])` | ||
### `q.start([cb])` | ||
cb, if passed, will be called when the queue empties or when an error occurs. | ||
#### `q.stop()` | ||
### `q.stop()` | ||
Stops the queue. can be resumed with `q.start()`. | ||
#### `q.end([err])` | ||
### `q.end([err])` | ||
Stop and empty the queue immediately. | ||
@@ -121,23 +120,23 @@ | ||
Mozilla has docs on how these methods work [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). 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.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` | ||
### `q.concurrency` | ||
Max number of jobs the queue should process concurrently, defaults to `Infinity`. | ||
#### `q.timeout` | ||
### `q.timeout` | ||
Milliseconds to wait for a job to execute its callback. | ||
#### `q.autostart` | ||
### `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.length` | ||
### `q.length` | ||
Jobs pending + jobs to process (readonly). | ||
@@ -147,12 +146,12 @@ | ||
#### `q.emit('success', result, job)` | ||
### `q.emit('success', result, job)` | ||
After a job executes its callback. | ||
#### `q.emit('error', err, job)` | ||
### `q.emit('error', err, job)` | ||
After a job passes an error to its callback. | ||
#### `q.emit('timeout', continue, job)` | ||
### `q.emit('timeout', continue, job)` | ||
After `q.timeout` milliseconds have elapsed and a job has not executed its callback. | ||
#### `q.emit('end'[, err])` | ||
### `q.emit('end'[, err])` | ||
After all jobs have been processed | ||
@@ -162,17 +161,19 @@ | ||
The latest stable release is published to [npm](http://npmjs.org/queue). Abbreviated changelog below: | ||
* [4.3](https://github.com/jessetane/queue/archive/4.3.0.tar.gz) | ||
* Add promise support (@kwolfy) | ||
* [4.2](https://github.com/jessetane/queue/archive/4.2.0.tar.gz) | ||
* Unref timers on end | ||
* Unref timers on end | ||
* [4.1](https://github.com/jessetane/queue/archive/4.1.0.tar.gz) | ||
* Add autostart feature | ||
* Add autostart feature | ||
* [4.0](https://github.com/jessetane/queue/archive/4.0.0.tar.gz) | ||
* Change license to MIT | ||
* Change license to MIT | ||
* [3.1.x](https://github.com/jessetane/queue/archive/3.0.6.tar.gz) | ||
* Add .npmignore | ||
* Add .npmignore | ||
* [3.0.x](https://github.com/jessetane/queue/archive/3.0.6.tar.gz) | ||
* Change the default concurrency to `Infinity` | ||
* Allow `q.start()` to accept an optional callback executed on `q.emit('end')` | ||
* Change the default concurrency to `Infinity` | ||
* Allow `q.start()` to accept an optional callback executed on `q.emit('end')` | ||
* [2.x](https://github.com/jessetane/queue/archive/2.2.0.tar.gz) | ||
* Major api changes / not backwards compatible with 1.x | ||
* Major api changes / not backwards compatible with 1.x | ||
* [1.x](https://github.com/jessetane/queue/archive/1.0.2.tar.gz) | ||
* Early prototype | ||
* Early prototype | ||
@@ -179,0 +180,0 @@ ## License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10868
155
181
0