diff --git a/README.md b/README.md index d973aa3..200d969 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -promiseq +PromiseQ [![Build Status](https://travis-ci.org/snailjs/promiseq.png?branch=master)](https://travis-ci.org/snailjs/promiseq) ======== Promise queue for node.js @@ -22,5 +22,19 @@ var queue = new PromiseQueue(workerCount) //make a job var job = function(){ - return -queue.push + return new P(function(resolve,reject){ + process.nextTick(resolve) + }) +} + +//add a single job +queue.push(job).then(function(){console.log('Job complete'})) + +//close the queue and listen for the drain +queue.close().then(funciton(){console.log('Queue closed and drained')}) +``` + +## Changelog + +### 0.1.0 +* Initial release diff --git a/helpers/PromiseQ.js b/helpers/PromiseQ.js index 60fb5a5..ef33dbe 100644 --- a/helpers/PromiseQ.js +++ b/helpers/PromiseQ.js @@ -22,7 +22,6 @@ var PromiseQueue = function(workers){ * @param {function} next */ var jobHandler = function(job,next){ - console.log('executing job',job) //call the function try to get the promise var rv = job.func() //make sure we have a promise diff --git a/test.js b/test.js deleted file mode 100644 index 202cf6e..0000000 --- a/test.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; -var async = require('async') -//var P = require('bluebird') - -//var PromiseQ = require('./index') - - -//var q = new PromiseQ(1) -var q = async.queue(function(job,next){ - console.log(job) - next() -},2) - -var job = function(){ - return new P(function(resolve){ - setTimeout(resolve,100) - }) -} - -process.nextTick(function(){ - for(var i = 0; i < 10; i++){ - console.log('queuing job ' + i) - q.push({blah: 'fuck it'},console.log) - } -}) - - -//console.log(q) -//q.close().then(function(){console.log('Queue drained')}) diff --git a/test/PromiseQ.test.js b/test/PromiseQ.test.js index 4f98d63..06fe560 100644 --- a/test/PromiseQ.test.js +++ b/test/PromiseQ.test.js @@ -4,6 +4,12 @@ var expect = require('chai').expect var PromiseQ = require('../helpers/PromiseQ') +var job = function(){ + return new P(function(resolve){ + setTimeout(resolve,10) + }) +} + describe('helpers/PromiseQ',function(){ it('should instantiate with worker count',function(){ @@ -14,15 +20,49 @@ describe('helpers/PromiseQ',function(){ var q = new PromiseQ() expect(q.workers).to.equal(require('os').cpus().length) }) - it.only('should allow execution of a job',function(done){ + it('should allow execution of a job',function(done){ this.timeout(10000) var q = new PromiseQ() - var job = function(){ + q.push(job).then(done).catch(done) + }) + it('should close the queue after all jobs finish',function(done){ + var q = new PromiseQ() + for(var i = 0; i < 10; i++) q.push(job) + q.close().then(done).catch(done) + }) + it('should bubble arguments to the resulting promise',function(done){ + var q = new PromiseQ() + q.push(function(){ return new P(function(resolve){ - - setTimeout(function(){console.log('resolving promise'); resolve()},10) + process.nextTick(function(){ + resolve(['foo','bar','baz']) + }) + }) + }) + .then(function(args){ + expect(args[0]).to.equal('foo') + expect(args[1]).to.equal('bar') + expect(args[2]).to.equal('baz') + done() + }) + .catch(done) + }) + it('should bubble errors',function(done){ + var q = new PromiseQ() + q.push(function(){ + return new P(function(resolve,reject){ + process.nextTick(function(){ + reject(new Error('foo')) + }) + }) + }) + .then(function(){ + done('error didnt bubble') + }) + .catch(function(err){ + expect(err).to.be.instanceOf(Error) + expect(err.message).to.equal('foo') + done() }) - } - q.push(job).then(done).catch(done) }) })