Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,26 @@ q.push(function (cb) {

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

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

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

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

// use the timeout feature to deal with jobs that
Expand Down Expand Up @@ -70,9 +65,10 @@ extraSlowJob.timeout = 500
q.push(extraSlowJob)

// 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)
q.on('success', function (job, firstResult) {
var args = Array.from(arguments)
var results = args.slice(1)
console.log('job successful, results:', results, 'source:', job.toString().replace(/\n/g, ''))
})

// begin processing, get notified on end / failure
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ Queue.prototype.start = function (cb) {
var timeoutId = null
var didTimeout = false
var resultIndex = null
var results = null
var timeout = job.timeout || this.timeout

function next (err, result) {
function next (err) {
if (once && self.session === session) {
once = false
self.pending--
Expand All @@ -108,9 +109,12 @@ Queue.prototype.start = function (cb) {
self.emit('error', err, job)
} else if (didTimeout === false) {
if (resultIndex !== null) {
self.results[resultIndex] = Array.prototype.slice.call(arguments, 1)
results = Array.prototype.slice.call(arguments, 1)
self.results[resultIndex] = results
}
if (self.listenerCount('success') > 0) {
self.emit.apply(self, ['success'].concat(job, results))
}
self.emit('success', result, job)
}

if (self.session === session) {
Expand All @@ -137,7 +141,7 @@ Queue.prototype.start = function (cb) {

if (this.results) {
resultIndex = this.results.length
this.results[resultIndex] = null
this.results[resultIndex] = []
}

this.pending++
Expand Down
25 changes: 10 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This module exports a class `Queue` that implements most of the `Array` API. Pas
## Example
`npm run example`
``` javascript
var queue = require('../')
var queue = require('queue')

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

Expand All @@ -28,31 +28,26 @@ q.push(function (cb) {

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

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

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

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

// use the timeout feature to deal with jobs that
Expand Down Expand Up @@ -88,17 +83,17 @@ extraSlowJob.timeout = 500
q.push(extraSlowJob)

// 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)
q.on('success', function (job, firstResult) {
var args = Array.from(arguments)
var results = args.slice(1)
console.log('job successful, results:', results, 'source:', job.toString().replace(/\n/g, ''))
})

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

```

## Install
Expand Down
23 changes: 16 additions & 7 deletions test/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ var tape = require('tape')
var queue = require('../')

tape('results', function (t) {
t.plan(2)
t.plan(6)

var i = 0
var expected = [
[42],
[3, 2, 1],
[],
['string']
]

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

Expand All @@ -24,13 +32,14 @@ tape('results', function (t) {
}, 10)
})

q.on('success', function () {
var results = Array.from(arguments).slice(1)
t.deepEqual(results, expected[i++])
})

q.start(function (err, results) {
t.error(err)
t.deepEqual(results, [
['string'],
[42],
[3, 2, 1],
[]
])
expected.unshift(expected.pop())
t.deepEqual(results, expected)
})
})