Comparing version 0.2.4 to 0.2.5
32
p.js
@@ -187,3 +187,3 @@ /*! | ||
runLater(function() { | ||
var r = resolverFor( p, x ); | ||
var r = resolverFor( p ); | ||
@@ -209,3 +209,3 @@ try { | ||
function resolverFor( promise, x ) { | ||
function resolverFor( promise ) { | ||
var done = false; | ||
@@ -219,9 +219,3 @@ | ||
done = true; | ||
if ( x && x === y ) { | ||
Settle( promise, FULFILLED, y ); | ||
} else { | ||
Resolve( promise, y ); | ||
} | ||
Resolve( promise, y ); | ||
} | ||
@@ -244,2 +238,7 @@ }, | ||
P.reject = reject; | ||
function reject( reason ) { | ||
return Settle( new Promise(), REJECTED, reason ); | ||
} | ||
function Promise() { | ||
@@ -332,8 +331,11 @@ this._state = 0; | ||
Promise.prototype.delay = function( ms ) { | ||
var p = this; | ||
var p2 = new Promise(); | ||
setTimeout(function() { | ||
Resolve( p2, p ); | ||
}, ms); | ||
return p2; | ||
var d = defer(); | ||
this.then(function( value ) { | ||
setTimeout(function() { | ||
d.resolve( value ); | ||
}, ms); | ||
}, d.reject); | ||
return d.promise; | ||
}; | ||
@@ -340,0 +342,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(e){if(typeof module!=="undefined"&&module&&module.exports){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else{P=e()}})(function(){"use strict";var e={f:null,n:null},t=e,n=false,r,i,o=c(typeof window)&&window||c(typeof worker)&&worker,u=c.call,f=c.apply;function a(){while(e.n){e=e.n;var t=e.f;e.f=null;t()}n=false}var s=function(e){t=t.n={f:e,n:null};if(!n){n=true;i(a,0)}};function c(e){return e==="object"||e==="function"}if(c(typeof process)&&process&&process.nextTick){i=process.nextTick}else if(c(typeof setImmediate)){i=o?function(e){o.setImmediate(e)}:function(e){setImmediate(e)}}else if(c(typeof MessageChannel)){r=new MessageChannel;r.port1.onmessage=a;i=function(){r.port2.postMessage(0)}}else{i=setTimeout;if(o&&c(typeof Image)&&Image){(function(){var e=0;var t=function(e){var t=new Image;t.onerror=e;t.src="data:image/png,"};try{t(function(){if(--e===0){i=t}});++e}catch(n){}e&&setTimeout(function(){e=0},0)})()}}function l(e,t){for(var n=0,r=e.length;n<r;++n){if(n in e){t(e[n],n)}}}function p(e){try{if(_.onerror){_.onerror(e)}else{throw e}}catch(t){setTimeout(function(){throw t},0)}}var v=0;var h=1;var d=2;function _(e){return e instanceof j?e:w(new j,e)}function m(e,t,n){if(e._state){return e}e._state=t;e._value=n;if(e._pending.length>0){l(e._pending,s)}e._pending=null;return e}function y(e,t){e._pending.push(t)}function w(e,t){if(e._state){return e}if(t instanceof j){if(t===e){m(e,d,new TypeError("You can't resolve a promise with itself"))}else if(t._state){m(e,t._state,t._value)}else{y(t,function(){m(e,t._state,t._value)})}}else if(t!==Object(t)){m(e,h,t)}else{s(function(){var n=g(e,t);try{var r=t.then;if(typeof r==="function"){u.call(r,t,n.resolve,n.reject)}else{m(e,h,t)}}catch(i){n.reject(i)}})}return e}function g(e,t){var n=false;return{promise:e,resolve:function(r){if(!n){n=true;if(t&&t===r){m(e,h,r)}else{w(e,r)}}},reject:function(t){if(!n){n=true;m(e,d,t)}}}}_.defer=T;function T(){return g(new j)}function j(){this._state=0;this._value=void 0;this._pending=[]}j.prototype.then=function(e,t){var n=typeof e==="function"?e:null;var r=typeof t==="function"?t:null;var i=this;var o=new j;function u(){var e,t=i._state===h?n:r;if(t!==null){try{e=t(i._value)}catch(u){m(o,d,u);return}w(o,e)}else{m(o,i._state,i._value)}}if(i._state===v){y(i,u)}else{s(u)}return o};j.prototype.done=function(e,t){var n=this;if(e||t){n=n.then(e,t)}n.then(null,p)};j.prototype.fail=function(e){return this.then(null,e)};j.prototype.spread=function(e,t){return this.then(e&&function(n){return x(n,[]).then(function(t){return f.call(e,void 0,t)},t)},t)};j.prototype.timeout=function(e,t){var n=this;var r=new j;if(n._state!==v){m(r,n._state,n._value)}else{var i=setTimeout(function(){m(r,d,new Error(t||"Timed out after "+e+" ms"))},e);y(n,function(){clearTimeout(i);m(r,n._state,n._value)})}return r};j.prototype.delay=function(e){var t=this;var n=new j;setTimeout(function(){w(n,t)},e);return n};j.prototype.inspect=function(){switch(this._state){case v:return{state:"pending"};case h:return{state:"fulfilled",value:this._value};case d:return{state:"rejected",reason:this._value};default:throw new TypeError("invalid state")}};function I(e){function t(t){return e(t,[])}function n(e){return _(e).then(t)}n._=e;return n}_.allSettled=I(k);function k(e,t){var n=0;var r=new j;l(e,function(e,i){var o=_(e);if(o._state===v){++n;y(o,function(){t[i]=o.inspect();if(--n===0){m(r,h,t)}})}else{t[i]=o.inspect()}});if(n===0){m(r,h,t)}return r}_.all=I(x);function x(e,t){var n=0;var r=T();l(e,function(e,i){var o=_(e);if(o._state===h){t[i]=o._value}else{++n;o.then(function(e){t[i]=e;if(--n===0){r.resolve(t)}},r.reject)}});if(n===0){r.resolve(t)}return r.promise}_.promised=E;function E(e){function t(t){return f.apply(e,t)}return function(){var e=x(arguments,[]);return x([this,e],[]).then(t)}}_.onerror=null;_.nextTick=function(e){s(function(){try{e()}catch(t){setTimeout(function(){throw t},0)}})};return _}); | ||
(function(e){if(typeof module!=="undefined"&&module&&module.exports){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else{P=e()}})(function(){"use strict";var e={f:null,n:null},t=e,n=false,r,i,o=c(typeof window)&&window||c(typeof worker)&&worker,u=c.call,f=c.apply;function a(){while(e.n){e=e.n;var t=e.f;e.f=null;t()}n=false}var s=function(e){t=t.n={f:e,n:null};if(!n){n=true;i(a,0)}};function c(e){return e==="object"||e==="function"}if(c(typeof process)&&process&&process.nextTick){i=process.nextTick}else if(c(typeof setImmediate)){i=o?function(e){o.setImmediate(e)}:function(e){setImmediate(e)}}else if(c(typeof MessageChannel)){r=new MessageChannel;r.port1.onmessage=a;i=function(){r.port2.postMessage(0)}}else{i=setTimeout;if(o&&c(typeof Image)&&Image){(function(){var e=0;var t=function(e){var t=new Image;t.onerror=e;t.src="data:image/png,"};try{t(function(){if(--e===0){i=t}});++e}catch(n){}e&&setTimeout(function(){e=0},0)})()}}function l(e,t){for(var n=0,r=e.length;n<r;++n){if(n in e){t(e[n],n)}}}function p(e){try{if(_.onerror){_.onerror(e)}else{throw e}}catch(t){setTimeout(function(){throw t},0)}}var v=0;var h=1;var d=2;function _(e){return e instanceof I?e:w(new I,e)}function m(e,t,n){if(e._state){return e}e._state=t;e._value=n;if(e._pending.length>0){l(e._pending,s)}e._pending=null;return e}function y(e,t){e._pending.push(t)}function w(e,t){if(e._state){return e}if(t instanceof I){if(t===e){m(e,d,new TypeError("You can't resolve a promise with itself"))}else if(t._state){m(e,t._state,t._value)}else{y(t,function(){m(e,t._state,t._value)})}}else if(t!==Object(t)){m(e,h,t)}else{s(function(){var n=g(e);try{var r=t.then;if(typeof r==="function"){u.call(r,t,n.resolve,n.reject)}else{m(e,h,t)}}catch(i){n.reject(i)}})}return e}function g(e){var t=false;return{promise:e,resolve:function(n){if(!t){t=true;w(e,n)}},reject:function(n){if(!t){t=true;m(e,d,n)}}}}_.defer=T;function T(){return g(new I)}_.reject=j;function j(e){return m(new I,d,e)}function I(){this._state=0;this._value=void 0;this._pending=[]}I.prototype.then=function(e,t){var n=typeof e==="function"?e:null;var r=typeof t==="function"?t:null;var i=this;var o=new I;function u(){var e,t=i._state===h?n:r;if(t!==null){try{e=t(i._value)}catch(u){m(o,d,u);return}w(o,e)}else{m(o,i._state,i._value)}}if(i._state===v){y(i,u)}else{s(u)}return o};I.prototype.done=function(e,t){var n=this;if(e||t){n=n.then(e,t)}n.then(null,p)};I.prototype.fail=function(e){return this.then(null,e)};I.prototype.spread=function(e,t){return this.then(e&&function(n){return E(n,[]).then(function(t){return f.call(e,void 0,t)},t)},t)};I.prototype.timeout=function(e,t){var n=this;var r=new I;if(n._state!==v){m(r,n._state,n._value)}else{var i=setTimeout(function(){m(r,d,new Error(t||"Timed out after "+e+" ms"))},e);y(n,function(){clearTimeout(i);m(r,n._state,n._value)})}return r};I.prototype.delay=function(e){var t=T();this.then(function(n){setTimeout(function(){t.resolve(n)},e)},t.reject);return t.promise};I.prototype.inspect=function(){switch(this._state){case v:return{state:"pending"};case h:return{state:"fulfilled",value:this._value};case d:return{state:"rejected",reason:this._value};default:throw new TypeError("invalid state")}};function k(e){function t(t){return e(t,[])}function n(e){return _(e).then(t)}n._=e;return n}_.allSettled=k(x);function x(e,t){var n=0;var r=new I;l(e,function(e,i){var o=_(e);if(o._state===v){++n;y(o,function(){t[i]=o.inspect();if(--n===0){m(r,h,t)}})}else{t[i]=o.inspect()}});if(n===0){m(r,h,t)}return r}_.all=k(E);function E(e,t){var n=0;var r=T();l(e,function(e,i){var o=_(e);if(o._state===h){t[i]=o._value}else{++n;o.then(function(e){t[i]=e;if(--n===0){r.resolve(t)}},r.reject)}});if(n===0){r.resolve(t)}return r.promise}_.promised=M;function M(e){function t(t){return f.apply(e,t)}return function(){var e=E(arguments,[]);return E([this,e],[]).then(t)}}_.onerror=null;_.nextTick=function(e){s(function(){try{e()}catch(t){setTimeout(function(){throw t},0)}})};return _}); |
{ | ||
"name": "p-promise", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "A simple Promises/A+ library.", | ||
@@ -5,0 +5,0 @@ "author": "Robert Katić <robert.katic@gmail.com> (https://github.com/rkatic)", |
@@ -24,2 +24,3 @@ [![Build Status](https://travis-ci.org/rkatic/p.png?branch=master)](https://travis-ci.org/rkatic/p) | ||
- `P(val)` | ||
- `P.reject(reason)` | ||
- `P.defer()` | ||
@@ -26,0 +27,0 @@ - `P.all(promises)` |
@@ -55,17 +55,24 @@ | ||
VALUES[ VALUES.length + 1 ] = "sparse"; | ||
VALUES.length++; | ||
function forEach( array, f ) { | ||
function map( array, f ) { | ||
var array2 = new Array(array.length|0); | ||
for ( var i = 0, l = array.length; i < l; ++i ) { | ||
if ( i in array ) { | ||
f( array[i], i, array ); | ||
array2[i] = f( array[i], i, array ); | ||
} | ||
} | ||
return array2; | ||
} | ||
function allValues( func ) { | ||
return P.all( map(VALUES, func) ); | ||
} | ||
describe("P function", function() { | ||
it("should return a promise", function() { | ||
forEach(VALUES, function( value ) { | ||
map(VALUES, function( value ) { | ||
expect( P(value).constructor.name ).to.be("Promise"); | ||
}) | ||
}); | ||
}); | ||
@@ -78,10 +85,6 @@ | ||
it("should fulfill with input if not a promise", function( done ) { | ||
var c = 0; | ||
forEach(VALUES, function( value ) { | ||
++c; | ||
P( value ).then(function() { | ||
if ( --c === 0 ) { | ||
done(); | ||
} | ||
it("should fulfill with input if not a promise", function() { | ||
return allValues(function( value ) { | ||
return P( value ).then(function( fulfilledValue ) { | ||
expect( fulfilledValue ).to.be( value ); | ||
}); | ||
@@ -95,5 +98,5 @@ }); | ||
it("on fulfillment", function() { | ||
forEach(VALUES, function( value ) { | ||
return allValues(function( value ) { | ||
var p = P( value ); | ||
p.then(function() { | ||
return p.then(function() { | ||
expect( p.inspect() ).to.be.eql( {state: "fulfilled", value: value} ); | ||
@@ -105,7 +108,8 @@ }); | ||
it("on rejection", function() { | ||
forEach(VALUES, function( reason ) { | ||
return allValues(function( reason ) { | ||
var d = P.defer(); | ||
expect( d.promise.inspect() ).to.be.eql( {state: "pending"} ); | ||
var p = d.promise; | ||
expect( p.inspect() ).to.be.eql( {state: "pending"} ); | ||
d.reject( reason ); | ||
d.promise.then(function() { | ||
return p.then( fail, function() { | ||
expect( p.inspect() ).to.be.eql( {state: "rejected", reason: reason} ); | ||
@@ -117,2 +121,13 @@ }); | ||
describe("reject", function() { | ||
it("returns a rejected promise", function() { | ||
return allValues(function( reason ) { | ||
return P.reject( reason ).then( fail, function( rejectedReason ) { | ||
expect( rejectedReason ).to.be( reason ); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe("all", function() { | ||
@@ -269,3 +284,3 @@ | ||
it("should dealy rejection", function() { | ||
it("should not dealy rejection", function() { | ||
var d = P.defer(); | ||
@@ -276,3 +291,3 @@ d.reject(1); | ||
setTimeout(function() { | ||
expect( promise.inspect().state ).to.be("pending"); | ||
expect( promise.inspect().state ).to.be("rejected"); | ||
}, 40); | ||
@@ -283,5 +298,5 @@ | ||
it("should delegate to faster passed promises, slowing them down", function() { | ||
it("should delay after fulfillment", function() { | ||
var p1 = P("foo").delay( 30 ); | ||
var p2 = p1.delay( 50 ); | ||
var p2 = p1.delay( 30 ); | ||
@@ -291,3 +306,3 @@ setTimeout(function() { | ||
expect( p2.inspect().state ).to.be("pending"); | ||
}, 40); | ||
}, 45); | ||
@@ -298,16 +313,2 @@ return p2.then(function( value ) { | ||
}); | ||
it("should delegate to slower passed promises, staying at their speed", function() { | ||
var p1 = P("foo").delay( 70 ); | ||
var p2 = p1.delay( 50 ); | ||
setTimeout(function() { | ||
expect( p1.inspect().state ).to.be("pending"); | ||
expect( p2.inspect().state ).to.be("pending"); | ||
}, 40); | ||
return p2.then(function( value ) { | ||
expect( value ).to.be("foo"); | ||
}); | ||
}); | ||
}); |
32023
17
979
39