Skip to content

Commit

Permalink
refactored example index.js
Browse files Browse the repository at this point in the history
incorporated examples of how to use the results array
  • Loading branch information
dcsim0n committed Jun 15, 2019
1 parent ae84c10 commit 369c5a0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 56 deletions.
43 changes: 15 additions & 28 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +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()
})

// jobs can send a result to the 'success' event
function asyncJob () {
return new Promise((resolve) => {
setTimeout(() => resolve('we waited 75ms'), 75)
})
}
q.push(function (cb) {
asyncJob()
.then((result) => cb(null, result))
.catch((error) => cb(error))
const result = 'three'
cb(null, result)
})

// use the timeout feature to deal with jobs that
Expand Down Expand Up @@ -84,12 +71,12 @@ q.push(extraSlowJob)

// get notified when jobs complete
q.on('success', function (result, job) {
result && console.log('job returned a result:', result)
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)
})
44 changes: 16 additions & 28 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +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()
})

// jobs can send a result to the 'success' event
function asyncJob(){
return new Promise((resolve)=>{
setTimeout(()=>resolve("we waited 75ms"),75)
})
}
q.push(function(cb){
asyncJob()
.then((result)=>cb(null, result))
.catch((error)=>cb(error))
const result = 'three'
cb(null, result)
})

// use the timeout feature to deal with jobs that
Expand Down Expand Up @@ -102,15 +89,16 @@ q.push(extraSlowJob)

// get notified when jobs complete
q.on('success', function (result, job) {
result && console.log("job returned a result:",result)
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 369c5a0

Please sign in to comment.