From f84430653127b99227c25c985c8099a7012df3d4 Mon Sep 17 00:00:00 2001 From: Dana Simmons Date: Mon, 10 Jun 2019 12:36:15 -0400 Subject: [PATCH 1/3] added an example for passing a result to the success handler --- readme.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/readme.md b/readme.md index b7d39df..cf6f023 100644 --- a/readme.md +++ b/readme.md @@ -56,6 +56,22 @@ q.splice(2, 0, function (cb) { cb() }) +// jobs can send a result to the 'success' event +q.push(function(callback){ + asyncJob() + .then(result=>callback(null,result)) + .catch(error=>callback(error)) +}) + +q.on('success',function(result,job){ + console.log('Result from asyncJob():' result) +}) + +q.on('error', function(error,job){ + console.log('Error from asyncJob():' error) + throw error +}) + // use the timeout feature to deal with jobs that // take too long or forget to execute a callback q.timeout = 100 From ae84c107b657f273031afc0c8dd04d5b1df674be Mon Sep 17 00:00:00 2001 From: Dana Simmons Date: Mon, 10 Jun 2019 15:49:27 -0400 Subject: [PATCH 2/3] added example of passing results to 'success' handler included an example of passing the result of a resolved promise to the 'success' event listener. Also shows how to pass an error from a rejected promise. Fixed some style errors. --- example/index.js | 13 +++++++++++++ readme.md | 21 +++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/example/index.js b/example/index.js index fb3ad47..f15551b 100644 --- a/example/index.js +++ b/example/index.js @@ -38,6 +38,18 @@ q.splice(2, 0, function (cb) { 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)) +}) + // use the timeout feature to deal with jobs that // take too long or forget to execute a callback q.timeout = 100 @@ -72,6 +84,7 @@ 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, '')) }) diff --git a/readme.md b/readme.md index cf6f023..7dfb20c 100644 --- a/readme.md +++ b/readme.md @@ -57,19 +57,15 @@ q.splice(2, 0, function (cb) { }) // jobs can send a result to the 'success' event -q.push(function(callback){ +function asyncJob(){ + return new Promise((resolve)=>{ + setTimeout(()=>resolve("we waited 75ms"),75) + }) +} +q.push(function(cb){ asyncJob() - .then(result=>callback(null,result)) - .catch(error=>callback(error)) -}) - -q.on('success',function(result,job){ - console.log('Result from asyncJob():' result) -}) - -q.on('error', function(error,job){ - console.log('Error from asyncJob():' error) - throw error + .then((result)=>cb(null, result)) + .catch((error)=>cb(error)) }) // use the timeout feature to deal with jobs that @@ -106,6 +102,7 @@ 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, '')) }) From 369c5a0852f0a895b67d3056c84aedf5a179e831 Mon Sep 17 00:00:00 2001 From: Dana Simmons Date: Sat, 15 Jun 2019 09:27:39 -0400 Subject: [PATCH 3/3] refactored example index.js incorporated examples of how to use the results array --- example/index.js | 43 +++++++++++++++---------------------------- readme.md | 44 ++++++++++++++++---------------------------- 2 files changed, 31 insertions(+), 56 deletions(-) diff --git a/example/index.js b/example/index.js index f15551b..817f2fe 100644 --- a/example/index.js +++ b/example/index.js @@ -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 @@ -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) }) diff --git a/readme.md b/readme.md index 7dfb20c..c249d34 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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