Comparing version 0.2.0 to 0.2.2
116
p.js
@@ -56,4 +56,6 @@ ;(function( factory ){ | ||
if ( ft(typeof setImmediate) ) { | ||
//runLater = wow ? | ||
if ( ot(typeof process) && process && process.nextTick ) { | ||
requestTick = process.nextTick; | ||
} else if ( ft(typeof setImmediate) ) { | ||
requestTick = wow ? | ||
@@ -67,6 +69,2 @@ function( cb ) { | ||
} else if ( ot(typeof process) && process && ft(typeof process.nextTick) ) { | ||
requestTick = process.nextTick; | ||
//runLater = process.nextTick; | ||
} else if ( ft(typeof MessageChannel) ) { | ||
@@ -112,3 +110,2 @@ channel = new MessageChannel(); | ||
isArray = Array.isArray || function( val ) { | ||
@@ -126,13 +123,2 @@ return !!val && toStr.call( val ) === "[object Array]"; | ||
function each( obj, cb ) { | ||
if ( isArray(obj) ) { | ||
forEach( obj, cb ); | ||
return; | ||
} | ||
for ( var prop in obj ) { | ||
cb( obj[prop], prop ); | ||
} | ||
} | ||
function reportError( error ) { | ||
@@ -171,3 +157,5 @@ try { | ||
forEach( p._pending, runLater ); | ||
if ( p._pending.length > 0 ) { | ||
forEach( p._pending, runLater ); | ||
} | ||
p._pending = null; | ||
@@ -178,3 +166,3 @@ | ||
function Append( p, f ) { | ||
function OnSettled( p, f ) { | ||
p._pending.push( f ); | ||
@@ -194,3 +182,3 @@ //p._tail = p._tail.n = { f: f, n: null }; | ||
} else { | ||
Append(x, function() { | ||
OnSettled(x, function() { | ||
Settle( p, x._state, x._value ); | ||
@@ -291,3 +279,3 @@ }); | ||
if ( p._state === PENDING ) { | ||
Append( p, onSettled ); | ||
OnSettled( p, onSettled ); | ||
@@ -311,7 +299,11 @@ } else { | ||
Promise.prototype.fail = function( eb ) { | ||
return this.then( null, eb ); | ||
}; | ||
Promise.prototype.spread = function( cb, eb ) { | ||
return this.then(cb && function( array ) { | ||
return all( array ).then(function( values ) { | ||
return all( array, [] ).then(function( values ) { | ||
return cb.apply( void 0, values ); | ||
}); | ||
}, eb); | ||
}, eb); | ||
@@ -333,3 +325,3 @@ }; | ||
Append(p, function() { | ||
OnSettled(p, function() { | ||
clearTimeout( timeoutId ); | ||
@@ -352,23 +344,71 @@ Settle( p2, p._state, p._value ); | ||
P.all = all; | ||
function all( promises ) { | ||
Promise.prototype.inspect = function() { | ||
switch ( this._state ) { | ||
case PENDING: return { state: "pending" }; | ||
case FULFILLED: return { state: "fulfilled", value: this._value }; | ||
case REJECTED: return { state: "rejected", reason: this._value }; | ||
default: throw new TypeError("invalid state"); | ||
} | ||
}; | ||
function valuesHandler( f ) { | ||
function handleValues( values ) { | ||
// Arrays are never considered thenables here, which isn't ideal, | ||
// but I prefer to speed up instead of supporting such silly cases. | ||
return isArray( values ) ? | ||
f( values, [] ) : | ||
P( values ).then(function( values ) { | ||
return f( values, [] ); | ||
}); | ||
} | ||
handleValues._ = f; | ||
return handleValues; | ||
} | ||
P.allSettled = valuesHandler( allSettled ); | ||
function allSettled( input, output ) { | ||
var waiting = 0; | ||
var d = defer(); | ||
each( promises, function( promise, index ) { | ||
var p = P( promise ); | ||
var outputPromise = new Promise(); | ||
forEach( input, function( x, index ) { | ||
var p = P( x ); | ||
if ( p._state === PENDING ) { | ||
++waiting; | ||
OnSettled(p, function() { | ||
output[ index ] = p.inspect(); | ||
if ( --waiting === 0 ) { | ||
Settle( outputPromise, FULFILLED, output ); | ||
} | ||
}); | ||
} else { | ||
output[ index ] = p.inspect(); | ||
} | ||
}); | ||
if ( waiting === 0 ) { | ||
Settle( outputPromise, FULFILLED, output ); | ||
} | ||
return outputPromise; | ||
} | ||
P.all = valuesHandler( all ); | ||
function all( input, output ) { | ||
var waiting = 0; | ||
var d = defer(); | ||
forEach( input, function( x, index ) { | ||
var p = P( x ); | ||
if ( p._state === FULFILLED ) { | ||
output[ index ] = p._value; | ||
} else { | ||
++waiting; | ||
p.then(function( value ) { | ||
promises[ index ] = value; | ||
output[ index ] = value; | ||
if ( --waiting === 0 ) { | ||
d.resolve( promises ); | ||
d.resolve( output ); | ||
} | ||
}, d.reject); | ||
} else { | ||
promises[ index ] = p._value; | ||
} | ||
}); | ||
if ( waiting === 0 ) { | ||
d.resolve( promises ); | ||
d.resolve( output ); | ||
} | ||
@@ -380,4 +420,2 @@ return d.promise; | ||
P.prototype = Promise.prototype; | ||
P.nextTick = function( f ) { | ||
@@ -396,5 +434,3 @@ runLater(function() { | ||
P._each = each; | ||
return P; | ||
}); |
@@ -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,o,i=s(typeof window)&&window||s(typeof worker)&&worker,u={}.toString,f;function a(){while(e.n){e=e.n;var t=e.f;e.f=null;t()}n=false}var c=function(e){t=t.n={f:e,n:null};if(!n){n=true;o(a,0)}};function s(e){return e==="object"||e==="function"}function l(e){return e==="function"}if(l(typeof setImmediate)){o=i?function(e){i.setImmediate(e)}:function(e){setImmediate(e)}}else if(s(typeof process)&&process&&l(typeof process.nextTick)){o=process.nextTick}else if(l(typeof MessageChannel)){r=new MessageChannel;r.port1.onmessage=a;o=function(){r.port2.postMessage(0)}}else{o=setTimeout;if(i&&s(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){o=t}});++e}catch(n){}e&&setTimeout(function(){e=0},0)})()}}f=Array.isArray||function(e){return!!e&&u.call(e)==="[object Array]"};function p(e,t){for(var n=0,r=e.length;n<r;++n){if(n in e){t(e[n],n)}}}function v(e,t){if(f(e)){p(e,t);return}for(var n in e){t(e[n],n)}}function h(e){try{if(d.onerror){d.onerror(e)}else{throw e}}catch(t){setTimeout(function(){throw t},0)}}var y=0;var m=1;var _=2;function d(e){return e instanceof k?e:T(new k,e)}function w(e,t,n){if(e._state){return e}e._state=t;e._value=n;p(e._pending,c);e._pending=null;return e}function g(e,t){e._pending.push(t)}function T(e,t){if(e._state){return e}if(t instanceof k){if(t._state){w(e,t._state,t._value)}else{g(t,function(){w(e,t._state,t._value)})}}else if(t!==Object(t)){w(e,m,t)}else{c(function(){try{var n=t.then;if(typeof n==="function"){var r=j(e,t);n.call(t,r.resolve,r.reject)}else{w(e,m,t)}}catch(o){w(e,_,o)}})}return e}function j(e,t){var n=false;return{promise:e,resolve:function(r){if(!n){n=true;if(t&&t===r){w(e,m,r)}else{T(e,r)}}},reject:function(t){if(!n){n=true;w(e,_,t)}}}}d.defer=I;function I(){return j(new k)}function k(){this._state=0;this._value=void 0;this._pending=[]}k.prototype.then=function(e,t){var n=typeof e==="function"?e:null;var r=typeof t==="function"?t:null;var o=this;var i=new k;function u(){var e,t=o._state===m?n:r;if(t!==null){try{e=t(o._value)}catch(u){w(i,_,u);return}T(i,e)}else{w(i,o._state,o._value)}}if(o._state===y){g(o,u)}else{c(u)}return i};k.prototype.done=function(e,t){var n=this;if(e||t){n=n.then(e,t)}n.then(null,h)};k.prototype.spread=function(e,t){return this.then(e&&function(t){return x(t).then(function(t){return e.apply(void 0,t)})},t)};k.prototype.timeout=function(e,t){var n=this;var r=new k;if(n._state!==y){w(r,n._state,n._value)}else{var o=setTimeout(function(){w(r,_,new Error(t||"Timed out after "+e+" ms"))},e);g(n,function(){clearTimeout(o);w(r,n._state,n._value)})}return r};k.prototype.delay=function(e){var t=this;var n=new k;setTimeout(function(){T(n,t)},e);return n};d.all=x;function x(e){var t=0;var n=I();v(e,function(r,o){var i=d(r);if(i._state===y){++t;i.then(function(r){e[o]=r;if(--t===0){n.resolve(e)}},n.reject)}else{e[o]=i._value}});if(t===0){n.resolve(e)}return n.promise}d.onerror=null;d.prototype=k.prototype;d.nextTick=function(e){c(function(){try{e()}catch(t){setTimeout(function(){throw t},0)}})};d._each=v;return d}); | ||
(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={}.toString,f;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"}function l(e){return e==="function"}if(c(typeof process)&&process&&process.nextTick){i=process.nextTick}else if(l(typeof setImmediate)){i=o?function(e){o.setImmediate(e)}:function(e){setImmediate(e)}}else if(l(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)})()}}f=Array.isArray||function(e){return!!e&&u.call(e)==="[object Array]"};function p(e,t){for(var n=0,r=e.length;n<r;++n){if(n in e){t(e[n],n)}}}function v(e){try{if(y.onerror){y.onerror(e)}else{throw e}}catch(t){setTimeout(function(){throw t},0)}}var h=0;var d=1;var _=2;function y(e){return e instanceof I?e:g(new I,e)}function m(e,t,n){if(e._state){return e}e._state=t;e._value=n;if(e._pending.length>0){p(e._pending,s)}e._pending=null;return e}function w(e,t){e._pending.push(t)}function g(e,t){if(e._state){return e}if(t instanceof I){if(t._state){m(e,t._state,t._value)}else{w(t,function(){m(e,t._state,t._value)})}}else if(t!==Object(t)){m(e,d,t)}else{s(function(){try{var n=t.then;if(typeof n==="function"){var r=T(e,t);n.call(t,r.resolve,r.reject)}else{m(e,d,t)}}catch(i){m(e,_,i)}})}return e}function T(e,t){var n=false;return{promise:e,resolve:function(r){if(!n){n=true;if(t&&t===r){m(e,d,r)}else{g(e,r)}}},reject:function(t){if(!n){n=true;m(e,_,t)}}}}y.defer=j;function j(){return T(new I)}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===d?n:r;if(t!==null){try{e=t(i._value)}catch(u){m(o,_,u);return}g(o,e)}else{m(o,i._state,i._value)}}if(i._state===h){w(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,v)};I.prototype.fail=function(e){return this.then(null,e)};I.prototype.spread=function(e,t){return this.then(e&&function(n){return b(n,[]).then(function(t){return e.apply(void 0,t)},t)},t)};I.prototype.timeout=function(e,t){var n=this;var r=new I;if(n._state!==h){m(r,n._state,n._value)}else{var i=setTimeout(function(){m(r,_,new Error(t||"Timed out after "+e+" ms"))},e);w(n,function(){clearTimeout(i);m(r,n._state,n._value)})}return r};I.prototype.delay=function(e){var t=this;var n=new I;setTimeout(function(){g(n,t)},e);return n};I.prototype.inspect=function(){switch(this._state){case h:return{state:"pending"};case d:return{state:"fulfilled",value:this._value};case _:return{state:"rejected",reason:this._value};default:throw new TypeError("invalid state")}};function k(e){function t(t){return f(t)?e(t,[]):y(t).then(function(t){return e(t,[])})}t._=e;return t}y.allSettled=k(x);function x(e,t){var n=0;var r=new I;p(e,function(e,i){var o=y(e);if(o._state===h){++n;w(o,function(){t[i]=o.inspect();if(--n===0){m(r,d,t)}})}else{t[i]=o.inspect()}});if(n===0){m(r,d,t)}return r}y.all=k(b);function b(e,t){var n=0;var r=j();p(e,function(e,i){var o=y(e);if(o._state===d){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}y.onerror=null;y.nextTick=function(e){s(function(){try{e()}catch(t){setTimeout(function(){throw t},0)}})};return y}); |
{ | ||
"name": "p-promise", | ||
"version": "0.2.0", | ||
"version": "0.2.2", | ||
"description": "A simple Promises/A+ library.", | ||
@@ -5,0 +5,0 @@ "author": "Robert Katić <robert.katic@gmail.com> (https://github.com/rkatic)", |
@@ -21,7 +21,8 @@ [![Build Status](https://travis-ci.org/rkatic/p.png?branch=master)](https://travis-ci.org/rkatic/p) | ||
P implements a subset of the [Q](https://github.com/kriskowal/q) API with few **differences**. | ||
P implements a subset of the [Q](https://github.com/kriskowal/q) API. | ||
- `P(val)` | ||
- `P.defer()` | ||
- `P.all(promises)` (array **or object** of promises) | ||
- `P.all(promises)` | ||
- `P.allSettled(promises)` | ||
- `P.onerror` | ||
@@ -35,3 +36,4 @@ - `P.nextTick(callback)` | ||
- `promise.spread(onFulfilled, onRejected)` | ||
- `promise.timeout(ms)` | ||
- `promise.timeout(ms, opt_timeoutMsg)` | ||
- `promise.delay(ms)` | ||
- `promise.inspect()` |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
18310
10
525
38
0