Permalink
Browse files

upgrade to bluebird

  • Loading branch information...
1 parent 504cf69 commit cb7d5135742d7083cb5f42fe297a2416c591cb71 @jsdnxx committed Dec 6, 2013
Showing with 51 additions and 51 deletions.
  1. +10 −10 index.js
  2. +2 −2 package.json
  3. +39 −39 test/test.js
View
20 index.js
@@ -1,4 +1,4 @@
-var Q = require('q')
+var Promise = require('bluebird')
var slice = Array.prototype.slice
function isPromise(p) {
@@ -7,16 +7,16 @@ function isPromise(p) {
function pcall (p) {
var t = typeof p
- if (t === 'boolean') return Q.resolve(p)
+ if (t === 'boolean') return Promise.cast(p)
if (t === 'function') return pcall(p())
if (isPromise(p)) return p
- return Q.resolve(Boolean(p))
+ return Promise.resolve(Boolean(p))
}
function or () {
var args = arguments;
var term = args[0]
- if (!term) return Q.reject(new Error('No terms'));
+ if (!term) return Promise.reject(new Error('No terms'));
if (args.length === 1) {
return pcall(term).then(Boolean)
}
@@ -31,7 +31,7 @@ function or () {
function and () {
var args = arguments;
var term = args[0]
- if (!term) return Q.reject(new Error('No terms'))
+ if (!term) return Promise.reject(new Error('No terms'))
if (args.length === 1) {
return pcall(term).then(Boolean)
}
@@ -47,36 +47,36 @@ function and () {
function some () {
var args = arguments;
var term = args[0]
- if (!term) return Q.reject(new Error('No terms'))
+ if (!term) return Promise.reject(new Error('No terms'))
if (args.length === 1) {
return pcall(term).then(Boolean)
}
var promises = slice.call(arguments).map(pcall)
- return Q.all(promises).then(function (results) {
+ return Promise.all(promises).then(function (results) {
return results.some(Boolean)
})
}
function every () {
var args = arguments;
var term = args[0]
- if (!term) return Q.reject(new Error('No terms'))
+ if (!term) return Promise.reject(new Error('No terms'))
if (args.length === 1) {
return pcall(term).then(Boolean)
}
var promises = slice.call(arguments).map(pcall)
- return Q.all(promises).then(function (results) {
+ return Promise.all(promises).then(function (results) {
return results.every(Boolean)
})
}
function not(term) {
- if (!term) return Q.reject(new Error('No term'))
+ if (!term) return Promise.reject(new Error('No term'))
return pcall(term).then(function (x) { return !x })
}
View
4 package.json
@@ -1,6 +1,6 @@
{
"name": "shortcircuit",
- "version": "0.5.2",
+ "version": "0.5.3",
"description": "asynchronous short circuit evaluation",
"keywords": [
"boolean",
@@ -33,6 +33,6 @@
"ski": "~1.0.0"
},
"dependencies": {
- "q": "~0.9.2"
+ "bluebird": "~0.11.5-0"
}
}
View
78 test/test.js
@@ -3,7 +3,7 @@ chai.should()
chai.use(require('chai-interface'))
var sinon = require('sinon')
chai.use(require('sinon-chai'))
-var Q = require('q')
+var Promise = require('bluebird')
var K = require('ski/k')
describe('shortcircuit-promise', function () {
@@ -29,27 +29,27 @@ describe('shortcircuit-promise', function () {
})
it('resolves false if any of the terms is false', function (done) {
- var t1 = K(Q.resolve(true))
- var t2 = K(Q.resolve(false))
+ var t1 = K(Promise.resolve(true))
+ var t2 = K(Promise.resolve(false))
and(t1, t2).then(function (val) {
val.should.equal(false)
}).then(done, done)
})
it('resolves true if all of the terms are true', function (done) {
- var t1 = K(Q.resolve(true))
- var t2 = K(Q.resolve(true))
+ var t1 = K(Promise.resolve(true))
+ var t2 = K(Promise.resolve(true))
and(t1, t2).then(function (val) {
val.should.equal(true)
}).then(done, done)
})
it('executes terms in serial', function (done) {
- var t1 = sinon.stub().returns(Q.resolve(true))
- var t2 = sinon.stub().returns(Q.resolve(true))
- var t3 = sinon.stub().returns(Q.resolve(true))
+ var t1 = sinon.stub().returns(Promise.resolve(true))
+ var t2 = sinon.stub().returns(Promise.resolve(true))
+ var t3 = sinon.stub().returns(Promise.resolve(true))
and(t1, t2, t3).then(function () {
t1.should.have.been.called
@@ -59,9 +59,9 @@ describe('shortcircuit-promise', function () {
})
it('only executes terms necessary', function (done) {
- var t1 = sinon.stub().returns(Q.resolve(true))
- var t2 = sinon.stub().returns(Q.resolve(false))
- var t3 = sinon.stub().returns(Q.resolve(true))
+ var t1 = sinon.stub().returns(Promise.resolve(true))
+ var t2 = sinon.stub().returns(Promise.resolve(false))
+ var t3 = sinon.stub().returns(Promise.resolve(true))
and(t1, t2, t3).then(function () {
t1.should.have.been.called
@@ -82,27 +82,27 @@ describe('shortcircuit-promise', function () {
})
it('resolves true if any of the terms is true', function (done) {
- var t1 = K(Q.resolve(false))
- var t2 = K(Q.resolve(true))
+ var t1 = K(Promise.resolve(false))
+ var t2 = K(Promise.resolve(true))
or(t1, t2).then(function (val) {
val.should.equal(true)
}).then(done, done)
})
it('resolves false if all of the terms are false', function (done) {
- var t1 = K(Q.resolve(false))
- var t2 = K(Q.resolve(false))
+ var t1 = K(Promise.resolve(false))
+ var t2 = K(Promise.resolve(false))
or(t1, t2).then(function (val) {
val.should.equal(false)
}).then(done, done)
})
it('executes terms in serial', function (done) {
- var t1 = sinon.stub().returns(Q.resolve(false))
- var t2 = sinon.stub().returns(Q.resolve(false))
- var t3 = sinon.stub().returns(Q.resolve(false))
+ var t1 = sinon.stub().returns(Promise.resolve(false))
+ var t2 = sinon.stub().returns(Promise.resolve(false))
+ var t3 = sinon.stub().returns(Promise.resolve(false))
or(t1, t2, t3).then(function () {
t1.should.have.been.called
@@ -112,9 +112,9 @@ describe('shortcircuit-promise', function () {
})
it('only executes terms necessary', function (done) {
- var t1 = sinon.stub().returns(Q.resolve(false))
- var t2 = sinon.stub().returns(Q.resolve(true))
- var t3 = sinon.stub().returns(Q.resolve(false))
+ var t1 = sinon.stub().returns(Promise.resolve(false))
+ var t2 = sinon.stub().returns(Promise.resolve(true))
+ var t3 = sinon.stub().returns(Promise.resolve(false))
or(t1, t2, t3).then(function () {
t1.should.have.been.called
@@ -125,8 +125,8 @@ describe('shortcircuit-promise', function () {
it('is rejected if any of the executed terms is rejected', function (done) {
var err = new Error('foo')
- var t1 = sinon.stub().returns(Q.resolve(false))
- var t2 = sinon.stub().returns(Q.reject(err))
+ var t1 = sinon.stub().returns(Promise.resolve(false))
+ var t2 = sinon.stub().returns(Promise.reject(err))
or(t1, t2).then(function () {
done(new Error('should not resolve'))
@@ -145,11 +145,11 @@ describe('shortcircuit-promise', function () {
})
it('returns true when the term is false', function (done) {
- not(Q.resolve(false)).then(function (val) { val.should.equal(true)}).then(done, done)
+ not(Promise.resolve(false)).then(function (val) { val.should.equal(true)}).then(done, done)
})
it('returns false when the term is true', function (done) {
- not(Q.resolve(true)).then(function (val) { val.should.equal(false)}).then(done, done)
+ not(Promise.resolve(true)).then(function (val) { val.should.equal(false)}).then(done, done)
})
})
@@ -163,27 +163,27 @@ describe('shortcircuit-promise', function () {
})
it('resolves true if any of the terms is true', function (done) {
- var t1 = K(Q.resolve(true))
- var t2 = K(Q.resolve(false))
+ var t1 = K(Promise.resolve(true))
+ var t2 = K(Promise.resolve(false))
some(t1, t2).then(function (val) {
val.should.equal(true)
}).then(done, done)
})
it('resolves false if all of the terms are false', function (done) {
- var t1 = K(Q.resolve(false))
- var t2 = K(Q.resolve(false))
+ var t1 = K(Promise.resolve(false))
+ var t2 = K(Promise.resolve(false))
some(t1, t2).then(function (val) {
val.should.equal(false)
}).then(done, done)
})
it('executes all terms in parallel', function (done) {
- var t1 = sinon.stub().returns(Q.resolve(true))
- var t2 = sinon.stub().returns(Q.resolve(false))
- var t3 = sinon.stub().returns(Q.resolve(true))
+ var t1 = sinon.stub().returns(Promise.resolve(true))
+ var t2 = sinon.stub().returns(Promise.resolve(false))
+ var t3 = sinon.stub().returns(Promise.resolve(true))
some(t1, t2, t3).then(function () {
t1.should.have.been.called
@@ -204,27 +204,27 @@ describe('shortcircuit-promise', function () {
})
it('resolves false if any of the terms is false', function (done) {
- var t1 = K(Q.resolve(true))
- var t2 = K(Q.resolve(false))
+ var t1 = K(Promise.resolve(true))
+ var t2 = K(Promise.resolve(false))
every(t1, t2).then(function (val) {
val.should.equal(false)
}).then(done, done)
})
it('resolves true if all of the terms are true', function (done) {
- var t1 = K(Q.resolve(true))
- var t2 = K(Q.resolve(true))
+ var t1 = K(Promise.resolve(true))
+ var t2 = K(Promise.resolve(true))
every(t1, t2).then(function (val) {
val.should.equal(true)
}).then(done, done)
})
it('executes all terms in parallel', function (done) {
- var t1 = sinon.stub().returns(Q.resolve(true))
- var t2 = sinon.stub().returns(Q.resolve(false))
- var t3 = sinon.stub().returns(Q.resolve(true))
+ var t1 = sinon.stub().returns(Promise.resolve(true))
+ var t2 = sinon.stub().returns(Promise.resolve(false))
+ var t3 = sinon.stub().returns(Promise.resolve(true))
every(t1, t2, t3).then(function () {
t1.should.have.been.called

0 comments on commit cb7d513

Please sign in to comment.