Skip to content

Commit

Permalink
Merge pull request #64 from dcsim0n/callback_result_example
Browse files Browse the repository at this point in the history
added an example for passing a result to the success handler
  • Loading branch information
jessetane committed Jun 18, 2019
2 parents 0b663a8 + 369c5a0 commit 691f70e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
30 changes: 15 additions & 15 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
var queue = require('../')

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

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

q.push(
function (cb) {
results.push('four')
cb()
const result = 'four'
cb(null, result)
},
function (cb) {
results.push('five')
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) {
results.push('one')
resolve()
const result = 'one'
resolve(result)
})
})

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

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

// use the timeout feature to deal with jobs that
Expand Down Expand Up @@ -73,10 +72,11 @@ 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)
})

// begin processing, get notified on end / failure
q.start(function (err) {
if (err) throw err
console.log('all done:', results)
console.log('all done:', q.results)
})
31 changes: 16 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,41 @@ This module exports a class `Queue` that implements most of the `Array` API. Pas
``` javascript
var queue = require('../')

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

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

q.push(
function (cb) {
results.push('four')
cb()
const result = 'four'
cb(null, result)
},
function (cb) {
results.push('five')
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) {
results.push('one')
resolve()
const result = 'one'
resolve(result)
})
})

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

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

// use the timeout feature to deal with jobs that
Expand Down Expand Up @@ -91,13 +90,15 @@ 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)
})

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

```

## Install
Expand Down

0 comments on commit 691f70e

Please sign in to comment.