Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added an example for passing a result to the success handler #64

Merged
merged 3 commits into from
Jun 18, 2019
Merged
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
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)
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may be able to do q.push(asyncJob) here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree, you could just return a promise. If we want to demonstrate how to pass a result into a callback() then maybe a different approach would work for you.

Suggested change
})
const result = 'this is a long stream of data'
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