Comparing version 0.4.5 to 0.4.6
{ | ||
"name": "p-promise", | ||
"main": "p.js", | ||
"version": "0.4.5", | ||
"version": "0.4.6", | ||
"homepage": "https://github.com/rkatic/p", | ||
@@ -6,0 +6,0 @@ "authors": [ |
100
p.js
@@ -298,6 +298,8 @@ /*! | ||
var OP_CALL = -1; | ||
var OP_THEN = -2; | ||
var OP_MULTIPLE = -3; | ||
var OP_CALL = 0; | ||
var OP_THEN = -1; | ||
var OP_MULTIPLE = -2; | ||
var VOID = P(void 0); | ||
function DoneEb( e ) { | ||
@@ -330,9 +332,2 @@ if ( P.onerror ) { | ||
function HandleSettled( p ) { | ||
if ( p._pending ) { | ||
HandlePending( p, p._op, p._pending ); | ||
p._pending = null; | ||
} | ||
} | ||
function Fulfill( p, value ) { | ||
@@ -444,5 +439,12 @@ if ( p._state ) { | ||
function HandleSettled( p ) { | ||
if ( p._pending ) { | ||
HandlePending( p, p._op, p._pending ); | ||
p._pending = null; | ||
} | ||
} | ||
function HandlePending( p, op, pending ) { | ||
if ( op === OP_CALL ) { | ||
pending( p ); | ||
if ( op >= 0 ) { | ||
pending( p, op ); | ||
@@ -452,9 +454,6 @@ } else if ( op === OP_THEN ) { | ||
} else if ( op === OP_MULTIPLE ) { | ||
} else { | ||
for ( var i = 0, l = pending.length; i < l; i += 2 ) { | ||
HandlePending( p, pending[i], pending[i + 1] ); | ||
} | ||
} else { | ||
pending( p, op ); | ||
} | ||
@@ -591,2 +590,8 @@ } | ||
Promise.prototype._clone = function() { | ||
var promise = new Promise(); | ||
ResolveWithPromise( promise, this ); | ||
return promise; | ||
}; | ||
Promise.prototype.then = function( onFulfilled, onRejected ) { | ||
@@ -632,4 +637,17 @@ var promise = new Promise(); | ||
Promise.prototype.fin = function( cb ) { | ||
var p = this; | ||
var promise = p.then( _finally, _finally ); | ||
function _finally() { | ||
return P( cb() ).then(function() { | ||
Propagate( p, promise ); | ||
}); | ||
} | ||
return promise; | ||
}; | ||
Promise.prototype.spread = function( cb, eb ) { | ||
return this.then( all ).then(function( args ) { | ||
return this.then( _all ).then(function( args ) { | ||
return apply.call( cb, void 0, args ); | ||
@@ -640,8 +658,5 @@ }, eb); | ||
Promise.prototype.timeout = function( ms, msg ) { | ||
var promise = new Promise(); | ||
var promise = this._clone(); | ||
if ( this._state !== PENDING ) { | ||
Propagate( this, promise ); | ||
} else { | ||
if ( this._state === PENDING ) { | ||
var trace = P.longStackSupport ? getTrace() : null; | ||
@@ -651,7 +666,7 @@ var timeoutId = setTimeout(function() { | ||
Reject( promise, new Error(msg || "Timed out after " + ms + " ms") ); | ||
currentTrace = null; | ||
}, ms); | ||
OnSettled( this, OP_CALL, function( p ) { | ||
OnSettled( this, OP_CALL, function() { | ||
clearTimeout( timeoutId ); | ||
Propagate( p, promise ); | ||
}); | ||
@@ -673,3 +688,5 @@ } | ||
} else { | ||
Propagate( p, promise ); | ||
VOID.then(function() { | ||
Propagate( p, promise ); | ||
}); | ||
} | ||
@@ -682,7 +699,7 @@ }); | ||
Promise.prototype.all = function() { | ||
return this.then( all ); | ||
return this.then( _all ); | ||
}; | ||
Promise.prototype.allSettled = function() { | ||
return this.then( allSettled ); | ||
return this.then( _allSettled ); | ||
}; | ||
@@ -713,2 +730,8 @@ | ||
function allSettled( input ) { | ||
var promise = _allSettled( input ); | ||
// Ensure propagation doesn't overflew the stack. | ||
return promise._state ? promise : promise._clone(); | ||
} | ||
function _allSettled( input ) { | ||
var promise = new Promise(); | ||
@@ -745,2 +768,8 @@ var len = input.length; | ||
function all( input ) { | ||
var promise = _all( input ); | ||
// Ensure propagation doesn't overflew the stack. | ||
return promise._state ? promise : promise._clone(); | ||
} | ||
function _all( input ) { | ||
var promise = new Promise(); | ||
@@ -784,4 +813,4 @@ var len = input.length; | ||
P.spread = spread; | ||
function spread( value, cb, eb ) { | ||
return all( value ).then(function( args ) { | ||
function spread( values, cb, eb ) { | ||
return _all( values ).then(function( args ) { | ||
return apply.call( cb, void 0, args ); | ||
@@ -804,3 +833,3 @@ }, eb); | ||
} | ||
return all( thisAndArgs ).then( onFulfilled ); | ||
return _all( thisAndArgs ).then( onFulfilled ); | ||
}; | ||
@@ -830,12 +859,9 @@ } | ||
P.nextTick = function nextTick( task ) { | ||
var p = new Promise(); | ||
Fulfill( p, task ); | ||
p = p.then( callValue ); | ||
// We don't use .done to avoid P.onerror. | ||
var p = VOID.then(function() { | ||
task.call(); | ||
}); | ||
OnSettled( p, OP_CALL, ReportIfRejected ); | ||
}; | ||
function callValue( value ) { | ||
value.call(); | ||
} | ||
var pEndingLine = captureLine(); | ||
@@ -842,0 +868,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=i,n=a(),t,r=null;function i(e){if(!e.stack){try{throw e}catch(n){}}return e}if((new Error).stack){e=function(e){return e}}function o(){var n=e(new Error).stack;if(!n){return null}var t=[f(n,1)];if(r){t=t.concat(r);if(t.length===128){t.pop()}}return t}function u(e){var n=/at .+ \((.+):(\d+):(?:\d+)\)$/.exec(e)||/at ([^ ]+):(\d+):(?:\d+)$/.exec(e)||/@(.+):(\d+):(?:\d+)$/.exec(e);return n?{fileName:n[1],lineNumber:Number(n[2])}:null}function a(){var n=e(new Error).stack;if(!n){return 0}var r=n.split("\n");var i=r[0].indexOf("@")>0?r[1]:r[2];var o=u(i);if(!o){return 0}t=o.fileName;return o.lineNumber}function f(e,n){var t=e.split("\n");var r=[];for(var i=n|0,o=t.length;i<o;++i){var u=t[i];if(u&&!l(u)&&!c(u)){r.push(u)}}return r.join("\n")}function l(e){return e.indexOf("(module.js:")!==-1||e.indexOf("(node.js:")!==-1}function c(e){var r=u(e);return!!r&&r.fileName===t&&r.lineNumber>=n&&r.lineNumber<=hn}var s="\nFrom previous event:\n";function v(n){if(n instanceof Error){var t=n.stack;if(!t){t=e(n).stack}else if(~t.indexOf(s)){return}if(t){n.stack=[f(t,0)].concat(r||[]).join(s)}}}var p=N(typeof process)&&process!=null&&{}.toString.call(process)==="[object process]",d=typeof setImmediate==="function",h=N(typeof MutationObserver)&&MutationObserver||N(typeof WebKitMutationObserver)&&WebKitMutationObserver,_=new E,m=_,y=false,w=0,b=p?M:h?$(A):K(A),g=[],x=K(O),k,T,j=N.call,S=N.apply;m.next=_;function E(){this.a=null;this.b=null;this.next=null}function N(e){return e==="object"||e==="function"}function O(){if(g.length){throw g.shift()}}function A(){while(_!==m){var e=_=_.next;if(w>=1024){m.next=m.next.next}else{++w}var n=e.a;var t=e.b;e.a=null;e.b=null;nn(n,t)}y=false;r=null}function I(e,n){var t=m.next;if(t===_){m.next=t=new E;t.next=_}else{--w}m=t;t.a=e;t.b=n;if(!y){y=true;b()}}function M(){var e=process.domain;if(e){if(!T)T=(1,require)("domain");T.active=process.domain=null}if(y&&d){setImmediate(A)}else{process.nextTick(A)}if(e){T.active=process.domain=e}}function $(e){var n=1;var t=document.createTextNode("");var r=new h(e);r.observe(t,{characterData:true});return function(){n=-n;t.data=n}}function K(e){return function(){var n=setTimeout(r,0);var t=setInterval(r,50);function r(){clearTimeout(n);clearInterval(t);e()}}}if(p){k=function(e){r=null;b();throw e}}else{k=function(e){g.push(e);x()}}var W=0;var q=1;var D=2;var F=-1;var P=-2;var Y=-3;function z(e){if(C.onerror){(1,C.onerror)(e)}else{throw e}}function B(e){if(e._state===D){if(e._domain){e._domain.enter()}k(e._value)}}function C(e){return e instanceof fn?e:Q(new fn,e)}C.longStackSupport=false;function G(e){if(e._pending){Z(e,e._op,e._pending);e._pending=null}}function H(e,n){if(e._state){return}e._state=q;e._value=n;G(e)}function J(e,n){if(e._state){return}if(r){v(n)}e._state=D;e._value=n;if(p){e._domain=process.domain}G(e)}function L(e,n){if(n._state){return}n._state=e._state;n._value=e._value;n._domain=e._domain;G(n)}function Q(e,n){if(e._state){return e}if(n instanceof fn){R(e,n)}else{var t=typeof n;if(t==="object"&&n!==null||t==="function"){U(e,n)}else{H(e,n)}}return e}function R(e,n){if(n===e){J(e,new TypeError("You can't resolve a promise with itself"))}else if(n._state){L(n,e)}else{en(n,P,e)}}function U(e,n){var t=V(e,n);if(typeof t==="function"){X(on(e),t,n)}else{H(e,n)}}function V(e,n){try{return n.then}catch(t){J(e,t);return null}}function X(e,n,t){try{j.call(n,t,e.resolve,e.reject)}catch(r){e.reject(r)}}function Z(e,n,t){if(n===F){t(e)}else if(n===P){I(e,t)}else if(n===Y){for(var r=0,i=t.length;r<i;r+=2){Z(e,t[r],t[r+1])}}else{t(e,n)}}function en(e,n,t){if(e._state){Z(e,n,t)}else if(!e._pending){e._pending=t;e._op=n}else if(e._op===Y){e._pending.push(n,t)}else{e._pending=[e._op,e._pending,n,t];e._op=Y}}function nn(e,n){var t=e._domain||n._domain;r=n._trace;var i=e._state===q?n._cb:n._eb;n._cb=null;n._eb=null;n._domain=null;n._trace=null;if(i===null){L(e,n)}else if(t){if(!t._disposed){t.enter();tn(n,i,e._value);t.exit()}}else{tn(n,i,e._value)}}function tn(e,n,t){var r;try{r=n(t)}catch(i){J(e,i);return}Q(e,r)}function rn(e,n){var t=false;var i=C.longStackSupport?o():null;return function(o,u){if(!t){t=true;if(i){if(r){i=null}else{r=i}}if(o){J(e,n?o:u)}else{Q(e,u)}if(i){r=null}}}}function on(e){var n=rn(e,false);return{promise:e,resolve:function(e){n(false,e)},reject:function(e){n(true,e)}}}C.defer=un;function un(){return on(new fn)}C.reject=an;function an(e){var n=new fn;J(n,e);return n}function fn(){this._state=0;this._value=void 0;this._domain=null;this._cb=null;this._eb=null;this._op=0;this._pending=null;this._trace=null}fn.prototype.then=function(e,n){var t=new fn;t._cb=typeof e==="function"?e:null;t._eb=typeof n==="function"?n:null;if(C.longStackSupport){t._trace=o()}if(p){t._domain=process.domain}if(this._state){I(this,t)}else{en(this,P,t)}return t};fn.prototype.done=function(e,n){var t=this;if(e||n){t=t.then(e,n)}t=t.then(null,z);en(t,F,B)};fn.prototype.fail=function(e){return this.then(null,e)};fn.prototype.spread=function(e,n){return this.then(cn).then(function(n){return S.call(e,void 0,n)},n)};fn.prototype.timeout=function(e,n){var t=new fn;if(this._state!==W){L(this,t)}else{var i=C.longStackSupport?o():null;var u=setTimeout(function(){r=i;J(t,new Error(n||"Timed out after "+e+" ms"))},e);en(this,F,function(e){clearTimeout(u);L(e,t)})}return t};fn.prototype.delay=function(e){var n=new fn;en(this,F,function(t){if(t._state===q){setTimeout(function(){L(t,n)},e)}else{L(t,n)}});return n};fn.prototype.all=function(){return this.then(cn)};fn.prototype.allSettled=function(){return this.then(ln)};fn.prototype.inspect=function(){switch(this._state){case W:return{state:"pending"};case q:return{state:"fulfilled",value:this._value};case D:return{state:"rejected",reason:this._value};default:throw new TypeError("invalid state")}};fn.prototype.nodeify=function(e){if(e){this.done(function(n){e(null,n)},e);return void 0}else{return this}};C.allSettled=ln;function ln(e){var n=new fn;var t=e.length;if(typeof t!=="number"){J(n,new TypeError("input not array-like"));return n}var r=new Array(t);var i=t;function o(e,t){r[t]=e.inspect();if(--i===0){H(n,r)}}for(var u=0;u<t;++u){en(C(e[u]),u,o)}if(i===0){H(n,r)}return n}C.all=cn;function cn(e){var n=new fn;var t=e.length;if(typeof t!=="number"){J(n,new TypeError("input not array-like"));return n}var r=new Array(t);var i=t;function o(e,t){if(r!==null){if(e._state===D){r=null;L(e,n)}else{r[t]=e._value;if(--i===0){H(n,r)}}}}for(var u=0;u<t;++u){en(C(e[u]),u,o)}if(i===0){H(n,r)}return n}C.spread=sn;function sn(e,n,t){return cn(e).then(function(e){return S.call(n,void 0,e)},t)}C.promised=vn;function vn(e){function n(n){return j.apply(e,n)}return function(){var e=arguments.length;var t=new Array(e+1);t[0]=this;for(var r=0;r<e;++r){t[r+1]=arguments[r]}return cn(t).then(n)}}C.denodeify=pn;function pn(e){return function(){var n=new fn;var t=arguments.length;var r=new Array(t+1);r[t]=rn(n,true);while(t--){r[t]=arguments[t]}S.call(e,this,r);return n}}C.onerror=null;C.nextTick=function _n(e){var n=new fn;H(n,e);n=n.then(dn);en(n,F,B)};function dn(e){e.call()}var hn=a();return C}); | ||
(function(n){if(typeof module!=="undefined"&&module&&module.exports){module.exports=n()}else if(typeof define==="function"&&define.amd){define(n)}else{P=n()}})(function(){"use strict";var n=i,e=a(),t,r=null;function i(n){if(!n.stack){try{throw n}catch(e){}}return n}if((new Error).stack){n=function(n){return n}}function o(){var e=n(new Error).stack;if(!e){return null}var t=[f(e,1)];if(r){t=t.concat(r);if(t.length===128){t.pop()}}return t}function u(n){var e=/at .+ \((.+):(\d+):(?:\d+)\)$/.exec(n)||/at ([^ ]+):(\d+):(?:\d+)$/.exec(n)||/@(.+):(\d+):(?:\d+)$/.exec(n);return e?{fileName:e[1],lineNumber:Number(e[2])}:null}function a(){var e=n(new Error).stack;if(!e){return 0}var r=e.split("\n");var i=r[0].indexOf("@")>0?r[1]:r[2];var o=u(i);if(!o){return 0}t=o.fileName;return o.lineNumber}function f(n,e){var t=n.split("\n");var r=[];for(var i=e|0,o=t.length;i<o;++i){var u=t[i];if(u&&!l(u)&&!c(u)){r.push(u)}}return r.join("\n")}function l(n){return n.indexOf("(module.js:")!==-1||n.indexOf("(node.js:")!==-1}function c(n){var r=u(n);return!!r&&r.fileName===t&&r.lineNumber>=e&&r.lineNumber<=_e}var s="\nFrom previous event:\n";function v(e){if(e instanceof Error){var t=e.stack;if(!t){t=n(e).stack}else if(~t.indexOf(s)){return}if(t){e.stack=[f(t,0)].concat(r||[]).join(s)}}}var p=N(typeof process)&&process!=null&&{}.toString.call(process)==="[object process]",d=typeof setImmediate==="function",h=N(typeof MutationObserver)&&MutationObserver||N(typeof WebKitMutationObserver)&&WebKitMutationObserver,_=new E,m=_,y=false,w=0,b=p?M:h?$(A):K(A),g=[],x=K(O),k,T,j=N.call,S=N.apply;m.next=_;function E(){this.a=null;this.b=null;this.next=null}function N(n){return n==="object"||n==="function"}function O(){if(g.length){throw g.shift()}}function A(){while(_!==m){var n=_=_.next;if(w>=1024){m.next=m.next.next}else{++w}var e=n.a;var t=n.b;n.a=null;n.b=null;te(e,t)}y=false;r=null}function I(n,e){var t=m.next;if(t===_){m.next=t=new E;t.next=_}else{--w}m=t;t.a=n;t.b=e;if(!y){y=true;b()}}function M(){var n=process.domain;if(n){if(!T)T=(1,require)("domain");T.active=process.domain=null}if(y&&d){setImmediate(A)}else{process.nextTick(A)}if(n){T.active=process.domain=n}}function $(n){var e=1;var t=document.createTextNode("");var r=new h(n);r.observe(t,{characterData:true});return function(){e=-e;t.data=e}}function K(n){return function(){var e=setTimeout(r,0);var t=setInterval(r,50);function r(){clearTimeout(e);clearInterval(t);n()}}}if(p){k=function(n){r=null;b();throw n}}else{k=function(n){g.push(n);x()}}var W=0;var q=1;var D=2;var F=0;var P=-1;var Y=-2;var z=G(void 0);function B(n){if(G.onerror){(1,G.onerror)(n)}else{throw n}}function C(n){if(n._state===D){if(n._domain){n._domain.enter()}k(n._value)}}function G(n){return n instanceof fe?n:Q(new fe,n)}G.longStackSupport=false;function H(n,e){if(n._state){return}n._state=q;n._value=e;Z(n)}function J(n,e){if(n._state){return}if(r){v(e)}n._state=D;n._value=e;if(p){n._domain=process.domain}Z(n)}function L(n,e){if(e._state){return}e._state=n._state;e._value=n._value;e._domain=n._domain;Z(e)}function Q(n,e){if(n._state){return n}if(e instanceof fe){R(n,e)}else{var t=typeof e;if(t==="object"&&e!==null||t==="function"){U(n,e)}else{H(n,e)}}return n}function R(n,e){if(e===n){J(n,new TypeError("You can't resolve a promise with itself"))}else if(e._state){L(e,n)}else{ee(e,P,n)}}function U(n,e){var t=V(n,e);if(typeof t==="function"){X(oe(n),t,e)}else{H(n,e)}}function V(n,e){try{return e.then}catch(t){J(n,t);return null}}function X(n,e,t){try{j.call(e,t,n.resolve,n.reject)}catch(r){n.reject(r)}}function Z(n){if(n._pending){ne(n,n._op,n._pending);n._pending=null}}function ne(n,e,t){if(e>=0){t(n,e)}else if(e===P){I(n,t)}else{for(var r=0,i=t.length;r<i;r+=2){ne(n,t[r],t[r+1])}}}function ee(n,e,t){if(n._state){ne(n,e,t)}else if(!n._pending){n._pending=t;n._op=e}else if(n._op===Y){n._pending.push(e,t)}else{n._pending=[n._op,n._pending,e,t];n._op=Y}}function te(n,e){var t=n._domain||e._domain;r=e._trace;var i=n._state===q?e._cb:e._eb;e._cb=null;e._eb=null;e._domain=null;e._trace=null;if(i===null){L(n,e)}else if(t){if(!t._disposed){t.enter();re(e,i,n._value);t.exit()}}else{re(e,i,n._value)}}function re(n,e,t){var r;try{r=e(t)}catch(i){J(n,i);return}Q(n,r)}function ie(n,e){var t=false;var i=G.longStackSupport?o():null;return function(o,u){if(!t){t=true;if(i){if(r){i=null}else{r=i}}if(o){J(n,e?o:u)}else{Q(n,u)}if(i){r=null}}}}function oe(n){var e=ie(n,false);return{promise:n,resolve:function(n){e(false,n)},reject:function(n){e(true,n)}}}G.defer=ue;function ue(){return oe(new fe)}G.reject=ae;function ae(n){var e=new fe;J(e,n);return e}function fe(){this._state=0;this._value=void 0;this._domain=null;this._cb=null;this._eb=null;this._op=0;this._pending=null;this._trace=null}fe.prototype._clone=function(){var n=new fe;R(n,this);return n};fe.prototype.then=function(n,e){var t=new fe;t._cb=typeof n==="function"?n:null;t._eb=typeof e==="function"?e:null;if(G.longStackSupport){t._trace=o()}if(p){t._domain=process.domain}if(this._state){I(this,t)}else{ee(this,P,t)}return t};fe.prototype.done=function(n,e){var t=this;if(n||e){t=t.then(n,e)}t=t.then(null,B);ee(t,F,C)};fe.prototype.fail=function(n){return this.then(null,n)};fe.prototype.fin=function(n){var e=this;var t=e.then(r,r);function r(){return G(n()).then(function(){L(e,t)})}return t};fe.prototype.spread=function(n,e){return this.then(ve).then(function(e){return S.call(n,void 0,e)},e)};fe.prototype.timeout=function(n,e){var t=this._clone();if(this._state===W){var i=G.longStackSupport?o():null;var u=setTimeout(function(){r=i;J(t,new Error(e||"Timed out after "+n+" ms"));r=null},n);ee(this,F,function(){clearTimeout(u)})}return t};fe.prototype.delay=function(n){var e=new fe;ee(this,F,function(t){if(t._state===q){setTimeout(function(){L(t,e)},n)}else{z.then(function(){L(t,e)})}});return e};fe.prototype.all=function(){return this.then(ve)};fe.prototype.allSettled=function(){return this.then(ce)};fe.prototype.inspect=function(){switch(this._state){case W:return{state:"pending"};case q:return{state:"fulfilled",value:this._value};case D:return{state:"rejected",reason:this._value};default:throw new TypeError("invalid state")}};fe.prototype.nodeify=function(n){if(n){this.done(function(e){n(null,e)},n);return void 0}else{return this}};G.allSettled=le;function le(n){var e=ce(n);return e._state?e:e._clone()}function ce(n){var e=new fe;var t=n.length;if(typeof t!=="number"){J(e,new TypeError("input not array-like"));return e}var r=new Array(t);var i=t;function o(n,t){r[t]=n.inspect();if(--i===0){H(e,r)}}for(var u=0;u<t;++u){ee(G(n[u]),u,o)}if(i===0){H(e,r)}return e}G.all=se;function se(n){var e=ve(n);return e._state?e:e._clone()}function ve(n){var e=new fe;var t=n.length;if(typeof t!=="number"){J(e,new TypeError("input not array-like"));return e}var r=new Array(t);var i=t;function o(n,t){if(r!==null){if(n._state===D){r=null;L(n,e)}else{r[t]=n._value;if(--i===0){H(e,r)}}}}for(var u=0;u<t;++u){ee(G(n[u]),u,o)}if(i===0){H(e,r)}return e}G.spread=pe;function pe(n,e,t){return ve(n).then(function(n){return S.call(e,void 0,n)},t)}G.promised=de;function de(n){function e(e){return j.apply(n,e)}return function(){var n=arguments.length;var t=new Array(n+1);t[0]=this;for(var r=0;r<n;++r){t[r+1]=arguments[r]}return ve(t).then(e)}}G.denodeify=he;function he(n){return function(){var e=new fe;var t=arguments.length;var r=new Array(t+1);r[t]=ie(e,true);while(t--){r[t]=arguments[t]}S.call(n,this,r);return e}}G.onerror=null;G.nextTick=function me(n){var e=z.then(function(){n.call()});ee(e,F,C)};var _e=a();return G}); |
{ | ||
"name": "p-promise", | ||
"version": "0.4.5", | ||
"version": "0.4.6", | ||
"description": "A simple Promises/A+ library.", | ||
@@ -5,0 +5,0 @@ "author": "Robert Katić <robert.katic@gmail.com> (https://github.com/rkatic)", |
@@ -29,2 +29,3 @@ [![Build Status](https://travis-ci.org/rkatic/p.png?branch=master)](https://travis-ci.org/rkatic/p) | ||
- `P.allSettled(promises)` | ||
- `P.spread(promises, onFulfilled, onRejected)` :new: | ||
- `P.onerror` | ||
@@ -38,2 +39,3 @@ - `P.longStackSupport` :new: | ||
- `promise.done(onFulfilled, onRejected)` | ||
- `promise.fin(callback)` :new: | ||
- `promise.spread(onFulfilled, onRejected)` | ||
@@ -40,0 +42,0 @@ - `promise.all()` :new: |
@@ -1,2 +0,2 @@ | ||
--- AUTO GENERATED CONTENT --- (2014/6/31 4:10:24) | ||
--- AUTO GENERATED CONTENT --- (2014/7/4 14:39:17) | ||
withStackThrowing : not optimized : 0 | ||
@@ -21,5 +21,4 @@ getTrace : not optimized : 0 | ||
P : optimized : 1 | ||
HandleSettled : optimized : 3 | ||
Fulfill : optimized : 1 | ||
Reject : optimized : 2 | ||
Reject : optimized : 3 | ||
Propagate : optimized : 1 | ||
@@ -31,4 +30,5 @@ Resolve : optimized : 1 | ||
TryResolver : not optimized : 0 | ||
HandlePending : optimized : 1 | ||
OnSettled : optimized : 1 | ||
HandleSettled : optimized : 3 | ||
HandlePending : optimized : 3 | ||
OnSettled : optimized : 2 | ||
Then : optimized : 2 | ||
@@ -41,5 +41,7 @@ HandleCallback : not optimized : 0 | ||
Promise : optimized : 1 | ||
Promise::_clone : optimized : 1 | ||
Promise::then : optimized : 1 | ||
Promise::done : optimized : 2 | ||
Promise::fail : not optimized : 0 | ||
Promise::fin : optimized : 1 | ||
Promise::spread : optimized : 1 | ||
@@ -53,6 +55,7 @@ Promise::timeout : optimized : 1 | ||
allSettled : optimized : 1 | ||
all : optimized : 2 | ||
_allSettled : optimized : 1 | ||
all : optimized : 1 | ||
_all : optimized : 3 | ||
spread : not optimized : 0 | ||
promised : not optimized : 0 | ||
denodeify : optimized : 1 | ||
callValue : not optimized : 0 | ||
denodeify : optimized : 1 |
100
test/p.js
@@ -298,6 +298,8 @@ /*! | ||
var OP_CALL = -1; | ||
var OP_THEN = -2; | ||
var OP_MULTIPLE = -3; | ||
var OP_CALL = 0; | ||
var OP_THEN = -1; | ||
var OP_MULTIPLE = -2; | ||
var VOID = P(void 0); | ||
TRACE_FUNCTIONS.fn = DoneEb; function DoneEb( e ) { | ||
@@ -330,9 +332,2 @@ if ( P.onerror ) { | ||
TRACE_FUNCTIONS.fn = HandleSettled; function HandleSettled( p ) { | ||
if ( p._pending ) { | ||
HandlePending( p, p._op, p._pending ); | ||
p._pending = null; | ||
} | ||
} | ||
TRACE_FUNCTIONS.fn = Fulfill; function Fulfill( p, value ) { | ||
@@ -444,5 +439,12 @@ if ( p._state ) { | ||
TRACE_FUNCTIONS.fn = HandleSettled; function HandleSettled( p ) { | ||
if ( p._pending ) { | ||
HandlePending( p, p._op, p._pending ); | ||
p._pending = null; | ||
} | ||
} | ||
TRACE_FUNCTIONS.fn = HandlePending; function HandlePending( p, op, pending ) { | ||
if ( op === OP_CALL ) { | ||
pending( p ); | ||
if ( op >= 0 ) { | ||
pending( p, op ); | ||
@@ -452,9 +454,6 @@ } else if ( op === OP_THEN ) { | ||
} else if ( op === OP_MULTIPLE ) { | ||
} else { | ||
for ( var i = 0, l = pending.length; i < l; i += 2 ) { | ||
HandlePending( p, pending[i], pending[i + 1] ); | ||
} | ||
} else { | ||
pending( p, op ); | ||
} | ||
@@ -591,2 +590,8 @@ } | ||
TRACE_FUNCTIONS.nextName = "Promise::_clone"; TRACE_FUNCTIONS.fn = Promise.prototype._clone = function() { | ||
var promise = new Promise(); | ||
ResolveWithPromise( promise, this ); | ||
return promise; | ||
}; | ||
TRACE_FUNCTIONS.nextName = "Promise::then"; TRACE_FUNCTIONS.fn = Promise.prototype.then = function( onFulfilled, onRejected ) { | ||
@@ -632,4 +637,17 @@ var promise = new Promise(); | ||
TRACE_FUNCTIONS.nextName = "Promise::fin"; TRACE_FUNCTIONS.fn = Promise.prototype.fin = function( cb ) { | ||
var p = this; | ||
var promise = p.then( _finally, _finally ); | ||
TRACE_FUNCTIONS.fn = _finally; function _finally() { | ||
return P( cb() ).then(function() { | ||
Propagate( p, promise ); | ||
}); | ||
} | ||
return promise; | ||
}; | ||
TRACE_FUNCTIONS.nextName = "Promise::spread"; TRACE_FUNCTIONS.fn = Promise.prototype.spread = function( cb, eb ) { | ||
return this.then( all ).then(function( args ) { | ||
return this.then( _all ).then(function( args ) { | ||
return apply.call( cb, void 0, args ); | ||
@@ -640,8 +658,5 @@ }, eb); | ||
TRACE_FUNCTIONS.nextName = "Promise::timeout"; TRACE_FUNCTIONS.fn = Promise.prototype.timeout = function( ms, msg ) { | ||
var promise = new Promise(); | ||
var promise = this._clone(); | ||
if ( this._state !== PENDING ) { | ||
Propagate( this, promise ); | ||
} else { | ||
if ( this._state === PENDING ) { | ||
var trace = P.longStackSupport ? getTrace() : null; | ||
@@ -651,7 +666,7 @@ var timeoutId = setTimeout(function() { | ||
Reject( promise, new Error(msg || "Timed out after " + ms + " ms") ); | ||
currentTrace = null; | ||
}, ms); | ||
OnSettled( this, OP_CALL, function( p ) { | ||
OnSettled( this, OP_CALL, function() { | ||
clearTimeout( timeoutId ); | ||
Propagate( p, promise ); | ||
}); | ||
@@ -673,3 +688,5 @@ } | ||
} else { | ||
Propagate( p, promise ); | ||
VOID.then(function() { | ||
Propagate( p, promise ); | ||
}); | ||
} | ||
@@ -682,7 +699,7 @@ }); | ||
TRACE_FUNCTIONS.nextName = "Promise::all"; TRACE_FUNCTIONS.fn = Promise.prototype.all = function() { | ||
return this.then( all ); | ||
return this.then( _all ); | ||
}; | ||
TRACE_FUNCTIONS.nextName = "Promise::allSettled"; TRACE_FUNCTIONS.fn = Promise.prototype.allSettled = function() { | ||
return this.then( allSettled ); | ||
return this.then( _allSettled ); | ||
}; | ||
@@ -713,2 +730,8 @@ | ||
TRACE_FUNCTIONS.fn = allSettled; function allSettled( input ) { | ||
var promise = _allSettled( input ); | ||
// Ensure propagation doesn't overflew the stack. | ||
return promise._state ? promise : promise._clone(); | ||
} | ||
TRACE_FUNCTIONS.fn = _allSettled; function _allSettled( input ) { | ||
var promise = new Promise(); | ||
@@ -745,2 +768,8 @@ var len = input.length; | ||
TRACE_FUNCTIONS.fn = all; function all( input ) { | ||
var promise = _all( input ); | ||
// Ensure propagation doesn't overflew the stack. | ||
return promise._state ? promise : promise._clone(); | ||
} | ||
TRACE_FUNCTIONS.fn = _all; function _all( input ) { | ||
var promise = new Promise(); | ||
@@ -784,4 +813,4 @@ var len = input.length; | ||
P.spread = spread; | ||
TRACE_FUNCTIONS.fn = spread; function spread( value, cb, eb ) { | ||
return all( value ).then(function( args ) { | ||
TRACE_FUNCTIONS.fn = spread; function spread( values, cb, eb ) { | ||
return _all( values ).then(function( args ) { | ||
return apply.call( cb, void 0, args ); | ||
@@ -804,3 +833,3 @@ }, eb); | ||
} | ||
return all( thisAndArgs ).then( onFulfilled ); | ||
return _all( thisAndArgs ).then( onFulfilled ); | ||
}; | ||
@@ -830,12 +859,9 @@ } | ||
P.nextTick = function nextTick( task ) { | ||
var p = new Promise(); | ||
Fulfill( p, task ); | ||
p = p.then( callValue ); | ||
// We don't use .done to avoid P.onerror. | ||
var p = VOID.then(function() { | ||
task.call(); | ||
}); | ||
OnSettled( p, OP_CALL, ReportIfRejected ); | ||
}; | ||
TRACE_FUNCTIONS.fn = callValue; function callValue( value ) { | ||
value.call(); | ||
} | ||
var pEndingLine = captureLine(); | ||
@@ -842,0 +868,0 @@ |
175
test/test.js
@@ -162,2 +162,13 @@ (function(){ | ||
function deep( func ) { | ||
var d = P.defer(); | ||
var p = d.promise; | ||
var n = 10000; | ||
while ( n-- ) { | ||
p = func([ p ]); | ||
} | ||
d.promise = p; | ||
return d; | ||
} | ||
describe("all", function() { | ||
@@ -204,2 +215,16 @@ | ||
}); | ||
it("should resolve on deep resolved promise", function() { | ||
var d = deep( P.all ); | ||
d.resolve( 1 ); | ||
return d.promise; | ||
}); | ||
it("should reject on deep rejected promise", function() { | ||
var d = deep( P.all ); | ||
d.reject( 7 ); | ||
return d.promise.then(fail, function( reason ) { | ||
expect( reason ).to.be( 7 ); | ||
}); | ||
}); | ||
}); | ||
@@ -230,2 +255,14 @@ | ||
}); | ||
it("should resolve on deep resolved promise", function() { | ||
var d = deep( P.allSettled ); | ||
d.resolve( 1 ); | ||
return d.promise; | ||
}); | ||
it("should resolve on deep rejected promise", function() { | ||
var d = deep( P.allSettled ); | ||
d.reject( new Error("foo") ); | ||
return d.promise; | ||
}); | ||
}); | ||
@@ -289,2 +326,136 @@ | ||
describe("fin", function() { | ||
describe("when the promise is fulfilled", function() { | ||
it("should call the callback and fulfill with the original value", function() { | ||
var called = false; | ||
return P("foo") | ||
.fin(function() { | ||
called = true; | ||
return "boo"; | ||
}) | ||
.then(function( value ) { | ||
expect( called ).to.be( true ); | ||
expect( value ).to.be("foo"); | ||
}); | ||
}); | ||
describe("when the callback returns a promise", function() { | ||
describe("that is fulfilled", function() { | ||
it("should fulfill with the original value after the promise is settled", function() { | ||
var delayed = P("boo").delay(50); | ||
return P("foo") | ||
.fin(function() { | ||
return delayed; | ||
}) | ||
.then(function( value ) { | ||
expect( delayed.inspect() ).to.be.eql({ state: "fulfilled", value: "boo" }); | ||
expect( value ).to.be("foo"); | ||
}); | ||
}) | ||
}); | ||
describe("that is rejected", function() { | ||
it("should reject with this new reason", function() { | ||
var theError = new Error("boo"); | ||
return P("foo") | ||
.fin(function() { | ||
return P.reject( theError ); | ||
}) | ||
.then(fail, function( reason ) { | ||
expect( reason ).to.be( theError ); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe("when the callback throws an exception", function() { | ||
it("should reject with this new exception", function() { | ||
var theError = new Error("boo"); | ||
return P("foo") | ||
.fin(function() { | ||
throw theError; | ||
}) | ||
.then(fail, function( reason ) { | ||
expect( reason ).to.be( theError ); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe("when the promise is rejected", function () { | ||
var theError = new Error("nooo"); | ||
it("should call the callback and reject with the original reason", function() { | ||
var called = false; | ||
return P.reject( theError ) | ||
.fin(function() { | ||
called = true; | ||
return "boo"; | ||
}) | ||
.then(fail, function( reason ) { | ||
expect( called ).to.be( true ); | ||
expect( reason ).to.be( theError ); | ||
}); | ||
}); | ||
describe("when the callback returns a promise", function() { | ||
describe("that is fulfilled", function() { | ||
it("should reject with the original reason after the promise is settled", function() { | ||
var delayed = P("boo").delay(50); | ||
return P.reject( theError ) | ||
.fin(function() { | ||
return delayed; | ||
}) | ||
.then(fail, function( reason ) { | ||
expect( delayed.inspect() ).to.be.eql({ state: "fulfilled", value: "boo" }); | ||
expect( reason ).to.be( theError ); | ||
}); | ||
}) | ||
}); | ||
describe("that is rejected", function() { | ||
it("should reject with this new reason", function() { | ||
return P.reject( new Error("boo") ) | ||
.fin(function() { | ||
return P.reject( theError ); | ||
}) | ||
.then(fail, function( reason ) { | ||
expect( reason ).to.be( theError ); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe("when the callback throws an exception", function() { | ||
it("should reject with this new exception", function() { | ||
var theError = new Error("boo"); | ||
return P.reject( new Error("boo") ) | ||
.fin(function() { | ||
throw theError; | ||
}) | ||
.then(fail, function( reason ) { | ||
expect( reason ).to.be( theError ); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe("timeout", function() { | ||
@@ -337,3 +508,3 @@ | ||
expect( promise.inspect().state ).to.be("pending"); | ||
}, 40); | ||
}, 30); | ||
@@ -350,3 +521,3 @@ return promise; | ||
expect( promise.inspect().state ).to.be("rejected"); | ||
}, 40); | ||
}, 30); | ||
@@ -353,0 +524,0 @@ return promise.then( fail, function(){} ); |
2189259
33
3183
48