Comparing version 0.5.0 to 0.5.1
@@ -6,3 +6,3 @@ { | ||
"authors": ["Paul Mucur"], | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"main": "lib/pacta.js", | ||
@@ -9,0 +9,0 @@ "devDependencies": { |
@@ -279,4 +279,3 @@ /*global module, define, process, setImmediate, setTimeout */ | ||
/* Map over the given Promise a with (a -> Promise b), returning a new | ||
* Promise (Promise b). Map over that, thereby gaining access to the inner | ||
/* Map over the given Promise a calling (a -> Promise b) to return a new | ||
* Promise b. Map over that in order to get to the inner value of b and | ||
@@ -286,10 +285,16 @@ * resolve another promise with it. Return that promise as it is equivalent | ||
*/ | ||
this.map(f).map(function (pb) { | ||
pb.map(function (value) { | ||
promise.resolve(value); | ||
}); | ||
this.map(function (value) { | ||
try { | ||
var pb = f(value); | ||
pb.onRejected(function (reason) { | ||
promise.reject(reason); | ||
}); | ||
pb.map(function (value) { | ||
promise.resolve(value); | ||
}); | ||
pb.onRejected(function (reason) { | ||
promise.reject(reason); | ||
}); | ||
} catch (e) { | ||
promise.reject(e); | ||
} | ||
}); | ||
@@ -308,14 +313,19 @@ | ||
/* Same algorithm as above */ | ||
this.mapError(f).mapError(function (pb) { | ||
pb.map(function (value) { | ||
promise.resolve(value); | ||
}); | ||
this.mapError(function (reason) { | ||
try { | ||
var pb = f(reason); | ||
pb.onRejected(function (reason) { | ||
promise.reject(reason); | ||
}); | ||
pb.map(function (value) { | ||
promise.resolve(value); | ||
}); | ||
pb.onRejected(function (reason) { | ||
promise.reject(reason); | ||
}); | ||
} catch (e) { | ||
promise.reject(e); | ||
} | ||
}); | ||
this.map(function(value) { | ||
this.map(function (value) { | ||
promise.resolve(value); | ||
@@ -357,3 +367,5 @@ }); | ||
Promise.prototype.empty = function () { | ||
return Promise.of(this.value.empty ? this.value.empty() : this.value.constructor.empty()); | ||
return this.map(function (value) { | ||
return value.empty ? value.empty() : value.constructor.empty(); | ||
}); | ||
}; | ||
@@ -360,0 +372,0 @@ |
@@ -7,3 +7,3 @@ { | ||
"keywords": ["promises", "monad", "functor", "promises-aplus"], | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"main": "./lib/pacta.js", | ||
@@ -10,0 +10,0 @@ "dependencies": {}, |
# pacta [![Build Status](https://travis-ci.org/mudge/pacta.png?branch=master)](https://travis-ci.org/mudge/pacta) | ||
```javascript | ||
{ 'pacta': '0.5.0' } | ||
{ 'pacta': '0.5.1' } | ||
``` | ||
@@ -259,3 +259,3 @@ | ||
var promise = new Promise(); | ||
promise.reject("Type error at line 214"); | ||
promise.reject('Type error at line 214'); | ||
@@ -265,3 +265,3 @@ promise.mapError(function (x) { | ||
return "Error: " + x; | ||
return 'Error: ' + x; | ||
}); //=> Rejected promise with "Error: Type error at line 214" as reason | ||
@@ -401,9 +401,8 @@ ``` | ||
setTimeout(function() { | ||
if(Date.now() % 2 == 0) { | ||
p.reject("Request timed out"); | ||
setTimeout(function () { | ||
if (Date.now() % 2 === 0) { | ||
p.reject('Request timed out'); | ||
} else { | ||
p.resolve('This is a critical sentence.'); | ||
} | ||
else { | ||
p.resolve("This is a critical sentence."); | ||
} | ||
}, 2000); | ||
@@ -414,9 +413,9 @@ | ||
var getMessage = function(error) { | ||
if(error) { | ||
console.error("Error received: " + error); | ||
console.log("Retrying…"); | ||
var getMessage = function (error) { | ||
if (error) { | ||
console.error('Error received: ' + error); | ||
console.log('Retrying...'); | ||
} | ||
console.log("Sending request…"); | ||
console.log('Sending request...'); | ||
return criticalAjaxCallThatMayFail(); | ||
@@ -540,4 +539,6 @@ }; | ||
`mapError` and `chainError` were contributed by [Rodolphe | ||
Belouin](https://github.com/rbelouin). | ||
* Fixes to `chain`, `chainError` and `empty` were contributed by [Ben | ||
Schulz](https://github.com/benschulz); | ||
* `mapError` and `chainError` were contributed by [Rodolphe | ||
Belouin](https://github.com/rbelouin). | ||
@@ -544,0 +545,0 @@ ## Acknowledgements |
@@ -14,39 +14,29 @@ /*global describe, it, beforeEach, require, setTimeout */ | ||
describe('Promise', function () { | ||
var p, p2, p3, p4, p5, p6, p7; | ||
function emptyPromise() { | ||
return new Promise(); | ||
} | ||
beforeEach(function () { | ||
p = new Promise(); | ||
setTimeout(function () { | ||
p.resolve('foo'); | ||
}, 50); | ||
function rejectedPromise(reason) { | ||
var p = new Promise(); | ||
p2 = new Promise(); | ||
setTimeout(function () { | ||
p2.resolve('bar'); | ||
}, 25); | ||
if (reason === undefined) { | ||
reason = 'error'; | ||
} | ||
p3 = new Promise(); | ||
setTimeout(function () { | ||
p3.resolve('baz'); | ||
}, 75); | ||
p.reject(reason); | ||
p5 = new Promise(); | ||
setTimeout(function () { | ||
p5.reject('foo'); | ||
}, 50); | ||
return p; | ||
} | ||
p6 = new Promise(); | ||
setTimeout(function () { | ||
p6.reject('bar'); | ||
}, 25); | ||
function fulfilledPromise(value) { | ||
if (value === undefined) { | ||
value = 1; | ||
} | ||
p7 = new Promise(); | ||
setTimeout(function () { | ||
p7.reject('baz'); | ||
}, 25); | ||
}); | ||
return Promise.of(value); | ||
} | ||
describe('.of', function () { | ||
it('wraps a value in a new promise', function (done) { | ||
Promise.of(1).map(function (x) { | ||
fulfilledPromise(1).map(function (x) { | ||
assert.equal(1, x); | ||
@@ -60,18 +50,11 @@ done(); | ||
it('is pending for unfulfilled and unrejected promises', function () { | ||
p = new Promise(); | ||
assert.equal('pending', p.state()); | ||
assert.equal('pending', emptyPromise().state()); | ||
}); | ||
it('is fulfilled for fulfilled promises', function () { | ||
p = Promise.of(1); | ||
assert.equal('fulfilled', p.state()); | ||
assert.equal('fulfilled', fulfilledPromise().state()); | ||
}); | ||
it('is rejected for rejected promises', function () { | ||
p = new Promise(); | ||
p.reject('error'); | ||
assert.equal('rejected', p.state()); | ||
assert.equal('rejected', rejectedPromise().state()); | ||
}); | ||
@@ -82,3 +65,3 @@ }); | ||
it('resolves a promise with its final value', function () { | ||
p = new Promise(); | ||
var p = emptyPromise(); | ||
p.resolve(1); | ||
@@ -90,5 +73,5 @@ | ||
it('triggers any listeners for resolution', function (done) { | ||
var triggered = false; | ||
var triggered = false, | ||
p = emptyPromise(); | ||
p = new Promise(); | ||
p.map(function () { | ||
@@ -108,4 +91,3 @@ triggered = true; | ||
it('does nothing to rejected promises', function () { | ||
p = new Promise(); | ||
p.reject('error'); | ||
var p = rejectedPromise(); | ||
p.resolve(1); | ||
@@ -117,5 +99,5 @@ | ||
it('does not trigger listeners if the promise is rejected', function () { | ||
var triggered = false; | ||
var triggered = false, | ||
p = rejectedPromise(); | ||
p = new Promise(); | ||
p.reject('error'); | ||
@@ -133,3 +115,3 @@ p.map(function () { | ||
it('rejects a promise, setting a reason', function () { | ||
p = new Promise(); | ||
var p = emptyPromise(); | ||
p.reject('error'); | ||
@@ -141,3 +123,3 @@ | ||
it('does nothing to fulfilled promises', function () { | ||
p = Promise.of(1); | ||
var p = fulfilledPromise(); | ||
p.reject('error'); | ||
@@ -149,5 +131,5 @@ | ||
it('triggers onRejected listeners', function (done) { | ||
var triggered = false; | ||
var triggered = false, | ||
p = emptyPromise(); | ||
p = new Promise(); | ||
p.onRejected(function () { | ||
@@ -166,5 +148,5 @@ triggered = true; | ||
it('does not trigger onRejected listeners if already fulfilled', function () { | ||
var triggered = false; | ||
var triggered = false, | ||
p = fulfilledPromise(); | ||
p = Promise.of(1); | ||
p.onRejected(function () { | ||
@@ -181,4 +163,3 @@ triggered = true; | ||
it('binds a listener to be fired on rejection', function (done) { | ||
p = new Promise(); | ||
p.reject('error'); | ||
var p = rejectedPromise('error'); | ||
@@ -192,10 +173,8 @@ p.onRejected(function (reason) { | ||
it('can be used to recover from a rejection', function (done) { | ||
p = new Promise(); | ||
p.reject(new TypeError()); | ||
var p1 = rejectedPromise(new TypeError()), | ||
p2 = p1.onRejected(function () { | ||
assert.equal('rejected', p1.state()); | ||
return 'Some safe default'; | ||
}); | ||
p2 = p.onRejected(function () { | ||
assert.equal('rejected', p.state()); | ||
return 'Some safe default'; | ||
}); | ||
p2.map(function (x) { | ||
@@ -209,10 +188,8 @@ assert.equal('fulfilled', p2.state()); | ||
it('can chain failures', function (done) { | ||
p = new Promise(); | ||
p.reject(new TypeError()); | ||
var p1 = rejectedPromise(new TypeError()), | ||
p2 = p1.onRejected(function () { | ||
assert.equal('rejected', p1.state()); | ||
throw new TypeError(); | ||
}); | ||
p2 = p.onRejected(function () { | ||
assert.equal('rejected', p.state()); | ||
throw new TypeError(); | ||
}); | ||
p2.onRejected(function () { | ||
@@ -227,3 +204,3 @@ assert.equal('rejected', p2.state()); | ||
it('yields the value of the promise', function (done) { | ||
p.map(function (x) { | ||
fulfilledPromise('foo').map(function (x) { | ||
assert.equal('foo', x); | ||
@@ -235,2 +212,5 @@ done(); | ||
it('yields the value after resolution', function (done) { | ||
var p = emptyPromise(); | ||
setTimeout(function () { p.resolve('foo'); }, 50); | ||
p.map(function () { | ||
@@ -246,2 +226,4 @@ /* Promise is now resolved so map again... */ | ||
it('can be chained', function (done) { | ||
var p = fulfilledPromise('foo'); | ||
p.map(function (x) { | ||
@@ -256,3 +238,7 @@ return x + '!'; | ||
it('can be nested', function (done) { | ||
p.map(function (x) { | ||
var p1 = fulfilledPromise('foo'), | ||
p2 = fulfilledPromise('bar'), | ||
p3 = fulfilledPromise('baz'); | ||
p1.map(function (x) { | ||
p2.map(function (y) { | ||
@@ -270,10 +256,9 @@ p3.map(function (z) { | ||
it('encapsulates exceptions in rejections', function (done) { | ||
var exception = new TypeError(); | ||
var exception = new TypeError(), | ||
p = fulfilledPromise('foo').map(function () { | ||
throw exception; | ||
}); | ||
p4 = p.map(function () { | ||
throw exception; | ||
}); | ||
p4.onRejected(function (r) { | ||
assert.equal('rejected', p4.state()); | ||
p.onRejected(function (r) { | ||
assert.equal('rejected', p.state()); | ||
assert.equal(exception, r); | ||
@@ -285,3 +270,3 @@ done(); | ||
it('fulfils the identity property of a functor', function (done) { | ||
p.map(function (x) { | ||
fulfilledPromise('foo').map(function (x) { | ||
return x; | ||
@@ -298,3 +283,3 @@ }).map(function (x) { | ||
p.map(function (x) { return f(g(x)); }).map(function (x) { | ||
fulfilledPromise('foo').map(function (x) { return f(g(x)); }).map(function (x) { | ||
assert.equal('f(g(foo))', x); | ||
@@ -309,3 +294,3 @@ done(); | ||
p.map(g).map(f).map(function (x) { | ||
fulfilledPromise('foo').map(g).map(f).map(function (x) { | ||
assert.equal('f(g(foo))', x); | ||
@@ -319,3 +304,3 @@ done(); | ||
it('yields the reason of the promise', function (done) { | ||
p5.mapError(function (x) { | ||
rejectedPromise('foo').mapError(function (x) { | ||
assert.equal('foo', x); | ||
@@ -327,5 +312,8 @@ done(); | ||
it('yields the reason after rejection', function (done) { | ||
p5.mapError(function () { | ||
var p = emptyPromise(); | ||
setTimeout(function () { p.reject('foo'); }, 50); | ||
p.mapError(function () { | ||
/* Promise is now rejected so mapError again... */ | ||
p5.mapError(function (x) { | ||
p.mapError(function (x) { | ||
assert.equal('foo', x); | ||
@@ -338,3 +326,3 @@ done(); | ||
it('can be chained', function (done) { | ||
p5.mapError(function (x) { | ||
rejectedPromise('foo').mapError(function (x) { | ||
return x + '!'; | ||
@@ -348,5 +336,9 @@ }).mapError(function (y) { | ||
it('can be nested', function (done) { | ||
p5.mapError(function (x) { | ||
p6.mapError(function (y) { | ||
p7.mapError(function (z) { | ||
var p1 = rejectedPromise('foo'), | ||
p2 = rejectedPromise('bar'), | ||
p3 = rejectedPromise('baz'); | ||
p1.mapError(function (x) { | ||
p2.mapError(function (y) { | ||
p3.mapError(function (z) { | ||
assert.equal('foo', x); | ||
@@ -363,13 +355,13 @@ assert.equal('bar', y); | ||
var exception = new TypeError(), | ||
pe1 = new Promise(), | ||
pe2; | ||
p1 = emptyPromise(), | ||
p2; | ||
pe1.reject('foo'); | ||
p1.reject('foo'); | ||
pe2 = pe1.mapError(function () { | ||
p2 = p1.mapError(function () { | ||
throw exception; | ||
}); | ||
pe2.onRejected(function (r) { | ||
assert.equal('rejected', pe2.state()); | ||
p2.onRejected(function (r) { | ||
assert.equal('rejected', p2.state()); | ||
assert.equal(exception, r); | ||
@@ -381,3 +373,3 @@ done(); | ||
it('fulfils the identity property of a functor', function (done) { | ||
p5.mapError(function (x) { | ||
rejectedPromise('foo').mapError(function (x) { | ||
return x; | ||
@@ -394,3 +386,3 @@ }).mapError(function (x) { | ||
p5.mapError(function (x) { return f(g(x)); }).mapError(function (x) { | ||
rejectedPromise('foo').mapError(function (x) { return f(g(x)); }).mapError(function (x) { | ||
assert.equal('f(g(foo))', x); | ||
@@ -405,3 +397,3 @@ done(); | ||
p5.mapError(g).mapError(f).mapError(function (x) { | ||
rejectedPromise('foo').mapError(g).mapError(f).mapError(function (x) { | ||
assert.equal('f(g(foo))', x); | ||
@@ -415,3 +407,3 @@ done(); | ||
it('yields its value like #map', function (done) { | ||
p.then(function (x) { | ||
fulfilledPromise('foo').then(function (x) { | ||
assert.equal('foo', x); | ||
@@ -423,3 +415,3 @@ done(); | ||
it('can be chained when returning a value', function (done) { | ||
p.then(function (x) { | ||
fulfilledPromise('foo').then(function (x) { | ||
return x + '!'; | ||
@@ -433,4 +425,4 @@ }).then(function (x) { | ||
it('does not wrap a promise in a promise', function (done) { | ||
p.then(function (x) { | ||
return Promise.of(x); | ||
fulfilledPromise('foo').then(function (x) { | ||
return fulfilledPromise(x); | ||
}).map(function (x) { | ||
@@ -443,15 +435,13 @@ assert.equal('foo', x); | ||
it('always returns a promise', function () { | ||
assert.equal('object', typeof p.then()); | ||
assert.equal(Promise, fulfilledPromise('foo').then().constructor); | ||
}); | ||
it('returns a fulfilled promise with the return value of onRejected', function (done) { | ||
p = new Promise(); | ||
p.reject('foo'); | ||
var p1 = rejectedPromise('foo'), | ||
p2 = p1.then(function () { | ||
return 1; | ||
}, function () { | ||
return 'error'; | ||
}); | ||
p2 = p.then(function () { | ||
return 1; | ||
}, function () { | ||
return 'error'; | ||
}); | ||
p2.map(function (x) { | ||
@@ -465,8 +455,8 @@ assert.equal('error', x); | ||
it('assumes the return value of onFulfilled', function (done) { | ||
p = Promise.of('foo'); | ||
p2 = p.then(function () { | ||
return 1; | ||
}, function () { | ||
return 'error'; | ||
}); | ||
var p1 = fulfilledPromise('foo'), | ||
p2 = p1.then(function () { | ||
return 1; | ||
}, function () { | ||
return 'error'; | ||
}); | ||
@@ -483,7 +473,7 @@ p2.map(function (x) { | ||
it('fulfils the associativity property of semigroups #1', function (done) { | ||
p = Promise.of([1]); | ||
p2 = Promise.of([2]); | ||
p3 = Promise.of([3]); | ||
var p1 = fulfilledPromise([1]), | ||
p2 = fulfilledPromise([2]), | ||
p3 = fulfilledPromise([3]); | ||
p.concat(p2).concat(p3).map(function (x) { | ||
p1.concat(p2).concat(p3).map(function (x) { | ||
assert.equal(1, x[0]); | ||
@@ -497,7 +487,7 @@ assert.equal(2, x[1]); | ||
it('fulfils the associativity property of semigroups #2', function (done) { | ||
p = Promise.of([1]); | ||
p2 = Promise.of([2]); | ||
p3 = Promise.of([3]); | ||
var p1 = fulfilledPromise([1]), | ||
p2 = fulfilledPromise([2]), | ||
p3 = fulfilledPromise([3]); | ||
p.concat(p2.concat(p3)).map(function (x) { | ||
p1.concat(p2.concat(p3)).map(function (x) { | ||
assert.equal(1, x[0]); | ||
@@ -511,7 +501,7 @@ assert.equal(2, x[1]); | ||
it('fulfils the identity of a semigroup', function (done) { | ||
p = Promise.of([1]); | ||
p2 = Promise.of([2]); | ||
p3 = Promise.of([3]); | ||
var p1 = fulfilledPromise([1]), | ||
p2 = fulfilledPromise([2]), | ||
p3 = fulfilledPromise([3]); | ||
p.concat(p2).concat(p3).map(function (x) { | ||
p1.concat(p2).concat(p3).map(function (x) { | ||
return x; | ||
@@ -525,3 +515,7 @@ }).map(function (x) { | ||
it('concatenates any monoid including strings', function (done) { | ||
p.concat(p2).concat(p3).map(function (x) { | ||
var p1 = fulfilledPromise('foo'), | ||
p2 = fulfilledPromise('bar'), | ||
p3 = fulfilledPromise('baz'); | ||
p1.concat(p2).concat(p3).map(function (x) { | ||
assert.equal('foobarbaz', x); | ||
@@ -533,4 +527,6 @@ done(); | ||
it('is rejected if the first promise is rejected', function (done) { | ||
p.reject('Foo'); | ||
p.concat(p2).onRejected(function (reason) { | ||
var p1 = rejectedPromise('Foo'), | ||
p2 = fulfilledPromise('bar'); | ||
p1.concat(p2).onRejected(function (reason) { | ||
assert.equal('Foo', reason); | ||
@@ -542,4 +538,6 @@ done(); | ||
it('is rejected if the second promise is rejected', function (done) { | ||
p2.reject('Foo'); | ||
p.concat(p2).onRejected(function (reason) { | ||
var p1 = fulfilledPromise('foo'), | ||
p2 = rejectedPromise('Foo'); | ||
p1.concat(p2).onRejected(function (reason) { | ||
assert.equal('Foo', reason); | ||
@@ -551,5 +549,6 @@ done(); | ||
it('takes the first rejection if both promises are rejected', function (done) { | ||
p.reject('Foo'); | ||
p2.reject('Bar'); | ||
p.concat(p2).onRejected(function (reason) { | ||
var p1 = rejectedPromise('Foo'), | ||
p2 = rejectedPromise('Bar'); | ||
p1.concat(p2).onRejected(function (reason) { | ||
assert.equal('Foo', reason); | ||
@@ -563,6 +562,6 @@ done(); | ||
it('fulfils the associativity property of chain #1', function (done) { | ||
var f = function (x) { return Promise.of('f(' + x + ')'); }, | ||
g = function (x) { return Promise.of('g(' + x + ')'); }; | ||
var f = function (x) { return fulfilledPromise('f(' + x + ')'); }, | ||
g = function (x) { return fulfilledPromise('g(' + x + ')'); }; | ||
p.chain(f).chain(g).map(function (x) { | ||
fulfilledPromise('foo').chain(f).chain(g).map(function (x) { | ||
assert.equal('g(f(foo))', x); | ||
@@ -574,6 +573,6 @@ done(); | ||
it('fulfils the associativity property of chain #2', function (done) { | ||
var f = function (x) { return Promise.of('f(' + x + ')'); }, | ||
g = function (x) { return Promise.of('g(' + x + ')'); }; | ||
var f = function (x) { return fulfilledPromise('f(' + x + ')'); }, | ||
g = function (x) { return fulfilledPromise('g(' + x + ')'); }; | ||
p.chain(function (x) { return f(x).chain(g); }).map(function (x) { | ||
fulfilledPromise('foo').chain(function (x) { return f(x).chain(g); }).map(function (x) { | ||
assert.equal('g(f(foo))', x); | ||
@@ -583,2 +582,23 @@ done(); | ||
}); | ||
it('encapsulates exceptions in rejections', function (done) { | ||
var exception = new TypeError(), | ||
p = fulfilledPromise().chain(function () { throw exception; }); | ||
p.onRejected(function (r) { | ||
assert.equal('rejected', p.state()); | ||
assert.equal(exception, r); | ||
done(); | ||
}); | ||
}); | ||
it('rejects the returned promise, if f does not return a promise', function (done) { | ||
var p = fulfilledPromise().chain(function () { return 'not-a-promise'; }); | ||
p.onRejected(function (r) { | ||
assert.equal('rejected', p.state()); | ||
assert.equal(TypeError, r.constructor); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -588,6 +608,6 @@ | ||
it('fulfils the associativity property of chain #1', function (done) { | ||
var f = function (x) { var p = new Promise(); p.reject('f(' + x + ')'); return p; }, | ||
g = function (x) { var p = new Promise(); p.reject('g(' + x + ')'); return p; }; | ||
var f = function (x) { return rejectedPromise('f(' + x + ')'); }, | ||
g = function (x) { return rejectedPromise('g(' + x + ')'); }; | ||
p5.chainError(f).chainError(g).mapError(function (x) { | ||
rejectedPromise('foo').chainError(f).chainError(g).mapError(function (x) { | ||
assert.equal('g(f(foo))', x); | ||
@@ -599,6 +619,6 @@ done(); | ||
it('fulfils the associativity property of chain #2', function (done) { | ||
var f = function (x) { var p = new Promise(); p.reject('f(' + x + ')'); return p; }, | ||
g = function (x) { var p = new Promise(); p.reject('g(' + x + ')'); return p; }; | ||
var f = function (x) { return rejectedPromise('f(' + x + ')'); }, | ||
g = function (x) { return rejectedPromise('g(' + x + ')'); }; | ||
p5.chainError(function (x) { return f(x).chainError(g); }).mapError(function (x) { | ||
rejectedPromise('foo').chainError(function (x) { return f(x).chainError(g); }).mapError(function (x) { | ||
assert.equal('g(f(foo))', x); | ||
@@ -608,2 +628,23 @@ done(); | ||
}); | ||
it('encapsulates exceptions in rejections', function (done) { | ||
var exception = new TypeError(), | ||
p = rejectedPromise().chainError(function () { throw exception; }); | ||
p.onRejected(function (r) { | ||
assert.equal('rejected', p.state()); | ||
assert.equal(exception, r); | ||
done(); | ||
}); | ||
}); | ||
it('rejects the returned promise, if f does not return a promise', function (done) { | ||
var p = rejectedPromise().chainError(function () { return 'not-a-promise'; }); | ||
p.onRejected(function (r) { | ||
assert.equal('rejected', p.state()); | ||
assert.equal(TypeError, r.constructor); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -613,3 +654,3 @@ | ||
it('fulfils the identity property of applicative', function (done) { | ||
Promise.of(function (a) { return a; }).ap(p).map(function (x) { | ||
fulfilledPromise(function (a) { return a; }).ap(fulfilledPromise('foo')).map(function (x) { | ||
assert.equal('foo', x); | ||
@@ -621,7 +662,7 @@ done(); | ||
it('fulfils the composition property of applicative #1', function (done) { | ||
var u = Promise.of(function (x) { return 'u(' + x + ')'; }), | ||
v = Promise.of(function (x) { return 'v(' + x + ')'; }), | ||
w = Promise.of('foo'); | ||
var u = fulfilledPromise(function (x) { return 'u(' + x + ')'; }), | ||
v = fulfilledPromise(function (x) { return 'v(' + x + ')'; }), | ||
w = fulfilledPromise('foo'); | ||
Promise.of(function (f) { | ||
fulfilledPromise(function (f) { | ||
return function (g) { | ||
@@ -639,5 +680,5 @@ return function (x) { | ||
it('fulfils the composition property of applicative #2', function (done) { | ||
var u = Promise.of(function (x) { return 'u(' + x + ')'; }), | ||
v = Promise.of(function (x) { return 'v(' + x + ')'; }), | ||
w = Promise.of('foo'); | ||
var u = fulfilledPromise(function (x) { return 'u(' + x + ')'; }), | ||
v = fulfilledPromise(function (x) { return 'v(' + x + ')'; }), | ||
w = fulfilledPromise('foo'); | ||
@@ -653,3 +694,3 @@ u.ap(v.ap(w)).map(function (x) { | ||
Promise.of(f).ap(Promise.of('foo')).map(function (x) { | ||
fulfilledPromise(f).ap(fulfilledPromise('foo')).map(function (x) { | ||
assert.equal('f(foo)', x); | ||
@@ -663,3 +704,3 @@ done(); | ||
Promise.of(f('foo')).map(function (x) { | ||
fulfilledPromise(f('foo')).map(function (x) { | ||
assert.equal('f(foo)', x); | ||
@@ -671,6 +712,6 @@ done(); | ||
it('fulfils the interchange property of applicative #1', function (done) { | ||
var u = Promise.of(function (x) { return 'u(' + x + ')'; }), | ||
var u = fulfilledPromise(function (x) { return 'u(' + x + ')'; }), | ||
y = 'y'; | ||
u.ap(Promise.of(y)).map(function (x) { | ||
u.ap(fulfilledPromise(y)).map(function (x) { | ||
assert.equal('u(y)', x); | ||
@@ -682,6 +723,6 @@ done(); | ||
it('fulfils the interchange property of applicative #2', function (done) { | ||
var u = Promise.of(function (x) { return 'u(' + x + ')'; }), | ||
var u = fulfilledPromise(function (x) { return 'u(' + x + ')'; }), | ||
y = 'y'; | ||
Promise.of(function (f) { | ||
fulfilledPromise(function (f) { | ||
return f(y); | ||
@@ -697,3 +738,3 @@ }).ap(u).map(function (x) { | ||
it('conforms to the right identity', function (done) { | ||
p = Promise.of([1]); | ||
var p = fulfilledPromise([1]); | ||
@@ -707,3 +748,3 @@ p.concat(p.empty()).map(function (x) { | ||
it('conforms to the left identity', function (done) { | ||
p = Promise.of([1]); | ||
var p = fulfilledPromise([1]); | ||
@@ -715,2 +756,13 @@ p.empty().concat(p).map(function (x) { | ||
}); | ||
it('works with unresolved promises', function (done) { | ||
var p = emptyPromise(); | ||
p.concat(p.empty()).map(function (x) { | ||
assert.deepEqual([1], x); | ||
done(); | ||
}); | ||
p.resolve([1]); | ||
}); | ||
}); | ||
@@ -720,3 +772,7 @@ | ||
it('concatenates values into a list regardless of type', function (done) { | ||
p.conjoin(p2).conjoin(p3).map(function (x) { | ||
var p1 = fulfilledPromise('foo'), | ||
p2 = fulfilledPromise('bar'), | ||
p3 = fulfilledPromise('baz'); | ||
p1.conjoin(p2).conjoin(p3).map(function (x) { | ||
assert.deepEqual(['foo', 'bar', 'baz'], x); | ||
@@ -728,7 +784,7 @@ done(); | ||
it('concatenates values into a list even if already a list', function (done) { | ||
p = Promise.of([1]); | ||
p2 = Promise.of([2, 3]); | ||
p3 = Promise.of([4]); | ||
var p1 = fulfilledPromise([1]), | ||
p2 = fulfilledPromise([2, 3]), | ||
p3 = fulfilledPromise([4]); | ||
p.conjoin(p2).conjoin(p3).map(function (x) { | ||
p1.conjoin(p2).conjoin(p3).map(function (x) { | ||
assert.deepEqual([1, 2, 3, 4], x); | ||
@@ -740,5 +796,6 @@ done(); | ||
it('concatenates values of mixed types', function (done) { | ||
p2 = Promise.of([2, 3]); | ||
var p1 = fulfilledPromise('foo'), | ||
p2 = fulfilledPromise([2, 3]); | ||
p.conjoin(p2).map(function (x) { | ||
p1.conjoin(p2).map(function (x) { | ||
assert.deepEqual(['foo', 2, 3], x); | ||
@@ -752,6 +809,6 @@ done(); | ||
it('appends promises to a promise of an array', function (done) { | ||
p = Promise.of([1]); | ||
p2 = Promise.of(2); | ||
var p1 = fulfilledPromise([1]), | ||
p2 = fulfilledPromise(2); | ||
p.append(p2).map(function (x) { | ||
p1.append(p2).map(function (x) { | ||
assert.deepEqual([1, 2], x); | ||
@@ -763,6 +820,6 @@ done(); | ||
it('appends promises of arrays to arrays without joining them', function (done) { | ||
p = Promise.of([1]); | ||
p2 = Promise.of([2]); | ||
var p1 = fulfilledPromise([1]), | ||
p2 = fulfilledPromise([2]); | ||
p.append(p2).map(function (x) { | ||
p1.append(p2).map(function (x) { | ||
assert.deepEqual([1, [2]], x); | ||
@@ -774,8 +831,8 @@ done(); | ||
it('can be chained without nesting arrays', function (done) { | ||
p = Promise.of([]); | ||
p2 = Promise.of([1]); | ||
p3 = Promise.of([2, 3]); | ||
p4 = Promise.of([4]); | ||
var p1 = fulfilledPromise([]), | ||
p2 = fulfilledPromise([1]), | ||
p3 = fulfilledPromise([2, 3]), | ||
p4 = fulfilledPromise([4]); | ||
p.append(p2).append(p3).append(p4).map(function (x) { | ||
p1.append(p2).append(p3).append(p4).map(function (x) { | ||
assert.deepEqual([[1], [2, 3], [4]], x); | ||
@@ -789,5 +846,3 @@ done(); | ||
it('calls the given function with each value of the Promise', function (done) { | ||
p = Promise.of([1, 2, 3]); | ||
p.spread(function (x, y, z) { | ||
fulfilledPromise([1, 2, 3]).spread(function (x, y, z) { | ||
assert.equal(1, x); | ||
@@ -801,5 +856,3 @@ assert.equal(2, y); | ||
it('returns a promise with a single value', function (done) { | ||
p = Promise.of([1, 2, 3]); | ||
p.spread(function (x, y, z) { | ||
fulfilledPromise([1, 2, 3]).spread(function (x, y, z) { | ||
return x + y + z; | ||
@@ -815,5 +868,3 @@ }).map(function (x) { | ||
it('returns a new promise with the result', function (done) { | ||
p = Promise.of([[1], [2], [3]]); | ||
p.reduce(function (acc, e) { | ||
fulfilledPromise([[1], [2], [3]]).reduce(function (acc, e) { | ||
return acc.concat(e); | ||
@@ -827,5 +878,3 @@ }).map(function (x) { | ||
it('takes an optional initial value', function (done) { | ||
p = Promise.of([1, 2, 3]); | ||
p.reduce(function (acc, e) { | ||
fulfilledPromise([1, 2, 3]).reduce(function (acc, e) { | ||
return acc + e; | ||
@@ -832,0 +881,0 @@ }, 0).map(function (x) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94354
1252
557