Comparing version 0.0.14 to 0.1.0
@@ -12,11 +12,42 @@ | ||
function rejected( reason ) { | ||
var deferred = P.defer(); | ||
deferred.reject( reason ); | ||
return deferred.promise; | ||
} | ||
exports.reduceInOrder = function( promises, func ) { | ||
var accPromise; | ||
if ( arguments.length > 2 ) { | ||
accPromise = P( arguments[2] ); | ||
} | ||
each( promises, function( promise, index ) { | ||
accPromise = accPromise ? | ||
accPromise.then(function( acc ) { | ||
return P( promise ).then(function( value ) { | ||
return func( acc, value, index, promise ); | ||
}); | ||
}) : | ||
P( promise ); | ||
}); | ||
if ( !accPromise ) { | ||
throw new TypeError("reduce of empty array with no initial value"); | ||
} | ||
return accPromise; | ||
} | ||
exports.reduce = function( promises, func ) { | ||
var deferred = P.defer(); | ||
var waiting = 1; | ||
var acc; | ||
var accPromise; | ||
var finished = false; | ||
if ( arguments.length > 2 ) { | ||
acc = P( arguments[2] ); | ||
acc.then( null, reject, ALT, true ); | ||
++waiting; | ||
accPromise = P( arguments[2] ); | ||
accPromise.then( check, reject, ALT ); | ||
} | ||
@@ -26,7 +57,3 @@ | ||
if ( --waiting === 0 && !finished ) { | ||
if ( acc ) { | ||
deferred.resolve( acc ); | ||
} else { | ||
deferred.reject( new TypeError("reduce of empty array with no initial value") ); | ||
} | ||
deferred.resolve( accPromise ); | ||
} | ||
@@ -42,3 +69,3 @@ } | ||
finished || each( promises, function( promise, index ) { | ||
each( promises, function( promise, index ) { | ||
++waiting; | ||
@@ -50,4 +77,4 @@ P( promise ).then(function( value ) { | ||
acc = acc ? | ||
acc.then(function( acc ) { | ||
accPromise = accPromise ? | ||
accPromise.then(function( acc ) { | ||
return func( acc, value, index, promises ); | ||
@@ -57,3 +84,3 @@ }) : | ||
acc.then( check, reject, ALT, true ); | ||
accPromise.then( check, reject, ALT, true ); | ||
@@ -63,5 +90,79 @@ }, reject, ALT); | ||
check(); | ||
if ( !waiting ) { | ||
throw new TypeError("reduce of empty array with no initial value"); | ||
} | ||
return deferred.promise; | ||
}; | ||
exports.reduce2 = function( promises, func ) { | ||
var deferred = P.defer(); | ||
var waiting = 0; | ||
var finished = false; | ||
var haveValue = false; | ||
var lastValue, lastIndex; | ||
if ( arguments.length > 2 ) { | ||
++waiting; | ||
P( arguments[2] ).then( check, reject, ALT ); | ||
} | ||
function reject( reason ) { | ||
finished = true; | ||
lastValue = void 0; | ||
deferred.reject( reason ); | ||
} | ||
function check( value, index ) { | ||
var tmp, undef; | ||
if ( finished ) { | ||
return; | ||
} | ||
if ( !waiting && index === void 0 ) { | ||
deferred.fulfill( value ); | ||
return; | ||
} | ||
if ( haveValue ) { | ||
haveValue = false; | ||
if ( index === undef && lastIndex !== undef ) { | ||
tmp = value; | ||
value = lastValue; | ||
index = lastIndex; | ||
} else { | ||
tmp = lastValue; | ||
} | ||
lastValue = undef; | ||
try { | ||
tmp = func( tmp, value, index, promises ); | ||
} catch ( ex ) { | ||
reject( ex ); | ||
return; | ||
} | ||
P( tmp ).then( check, reject, ALT ); | ||
} else { | ||
haveValue = true; | ||
lastValue = value; | ||
lastIndex = index; | ||
} | ||
} | ||
each( promises, function( promise, index ) { | ||
++waiting; | ||
P( promise ).then(function( value ) { | ||
--waiting; | ||
check( value, index ); | ||
}, reject, ALT ); | ||
}); | ||
if ( !waiting ) { | ||
throw new TypeError("reduce of empty array with no initial value"); | ||
} | ||
return deferred.promise; | ||
} |
142
p.js
@@ -58,3 +58,3 @@ ;(function( factory ){ | ||
function runLater( f, couldThrow ) { | ||
var runLater = function( f, couldThrow ) { | ||
if ( nextNeedsTick && ++neededTicks > pendingTicks ) { | ||
@@ -66,3 +66,3 @@ ++pendingTicks; | ||
nextNeedsTick = couldThrow === true; | ||
} | ||
}; | ||
@@ -79,3 +79,4 @@ function ot( type ) { | ||
if ( ot(typeof process) && process && ft(typeof process.nextTick) ) { | ||
requestTick = process.nextTick; | ||
//requestTick = process.nextTick; | ||
runLater = process.nextTick; | ||
@@ -127,2 +128,12 @@ } else if ( wow && ft(typeof wow.setImmediate) ) { | ||
function reportError( error ) { | ||
runLater(function() { | ||
if ( P.onerror ) { | ||
P.onerror( error ); | ||
} else { | ||
throw error; | ||
} | ||
}, true); | ||
} | ||
isArray = Array.isArray || function( val ) { | ||
@@ -164,4 +175,3 @@ return !!val && toStr.call( val ) === "[object Array]"; | ||
var pending = [], | ||
rejected = false, | ||
promise = new Promise( then ), | ||
fulfilled = false, | ||
value; | ||
@@ -173,9 +183,4 @@ | ||
function onSettled() { | ||
var func = rejected ? onRejected : onFulfilled; | ||
var func = fulfilled ? onFulfilled : onRejected; | ||
if ( !def ) { | ||
func && func( value ); | ||
return; | ||
} | ||
if ( typeof func === "function" ) { | ||
@@ -185,13 +190,13 @@ try { | ||
} catch ( ex ) { | ||
def.reject( ex ); | ||
def ? def.reject( ex ) : reportError( ex ); | ||
return; | ||
} | ||
def.resolve( res ); | ||
def && def.resolve( res ); | ||
} else if ( rejected ) { | ||
def.reject( value ); | ||
} else if ( def ) { | ||
def.resolve( value, ALT, fulfilled ); | ||
} else { | ||
def.fulfill( value ); | ||
} else if ( !fulfilled ) { | ||
reportError( value ); | ||
} | ||
@@ -213,42 +218,48 @@ } | ||
function resolve( val ) { | ||
function resolve( x, alt, ok ) { | ||
if ( pending ) { | ||
if ( val instanceof Promise ) { | ||
val.then( fulfill, reject, ALT, true ); | ||
if ( alt === ALT ) { // settle | ||
fulfilled = !!ok; | ||
value = x; | ||
forEach( pending, runLater ); | ||
pending = null; | ||
} else if ( val && typeof val.then === "function" ) { | ||
runLater(function() { | ||
try { | ||
val.then( fulfill, reject ); | ||
} catch ( ex ) { | ||
reject( ex ); | ||
} else if ( x instanceof Promise ) { | ||
x.then( fulfill, reject, ALT, true ); | ||
return; | ||
} | ||
var then, type = typeof x; | ||
if ( type === "object" && x !== null || type === "function" ) { | ||
try { | ||
then = x.then; | ||
if ( typeof then === "function" ) { | ||
then.call( x, resolve, reject ); | ||
return; | ||
} | ||
}); | ||
} else { | ||
fulfill( val ); | ||
} catch ( ex ) { | ||
reject( ex ); | ||
return; | ||
} | ||
} | ||
fulfill( x ); | ||
} | ||
} | ||
function fulfill( val ) { | ||
if ( pending ) { | ||
promise.state = rejected ? "rejected" : "fulfilled"; | ||
promise.value = value = val; | ||
forEach( pending, runLater ); | ||
pending = null; | ||
} | ||
function fulfill( value ) { | ||
resolve( value, ALT, true ); | ||
} | ||
function reject( error ) { | ||
if ( pending ) { | ||
rejected = true; | ||
fulfill( error ); | ||
} | ||
function reject( reson ) { | ||
resolve( reson, ALT, false ); | ||
} | ||
return { | ||
promise: promise, | ||
promise: new Promise( then ), | ||
resolve: resolve, | ||
fulfill: fulfill, | ||
reject: reject | ||
@@ -261,20 +272,6 @@ }; | ||
this.then = then; | ||
this.state = "pending"; | ||
this.value = void 0; | ||
} | ||
Promise.prototype.done = function( cb, eb ) { | ||
var p = this; | ||
if ( cb || eb ) { | ||
p = p.then( cb, eb ); | ||
} | ||
p.then(null, function( error ) { | ||
runLater(function() { | ||
if ( P.onerror ) { | ||
P.onerror( error ); | ||
} else { | ||
throw error; | ||
} | ||
}, true); | ||
}, ALT); | ||
this.then( cb, eb, ALT ); | ||
}; | ||
@@ -294,5 +291,5 @@ | ||
this.when(function( value ) { | ||
this.then(function( value ) { | ||
clearTimeout( timeoutId ); | ||
def.fulfill( value ); | ||
def.resolve( value ); | ||
}, function( error ) { | ||
@@ -324,3 +321,3 @@ clearTimeout( timeoutId ); | ||
if ( --waiting === 0 ) { | ||
def.fulfill( promises ); | ||
def.resolve( promises ); | ||
} | ||
@@ -330,3 +327,3 @@ }, def.reject, ALT ); | ||
if ( waiting === 0 ) { | ||
def.fulfill( promises ); | ||
def.resolve( promises ); | ||
} | ||
@@ -336,21 +333,2 @@ return def.promise; | ||
// P.allResolved is DEPRECATED! | ||
P.allSettled = P.allResolved = allSettled; | ||
function allSettled( promises ) { | ||
var waiting = 1; | ||
var def = defer(); | ||
function callback() { | ||
if ( --waiting === 0 ) { | ||
def.fulfill( promises ); | ||
} | ||
} | ||
each( promises, function( promise, index ) { | ||
++waiting; | ||
promises[ index ] = promise = P( promise ); | ||
promise.then( callback, callback, ALT ); | ||
}); | ||
callback(); | ||
return def.promise; | ||
} | ||
P.onerror = null; | ||
@@ -364,4 +342,2 @@ | ||
P.ALT = ALT; | ||
P._each = each; | ||
@@ -368,0 +344,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,w:false,n:null},n=e,t=true,r=0,o=0,i,f,u=v(typeof window)&&window||v(typeof worker)&&worker,c=e.toString,l,s={};function a(){--r;if(e.n){if(!r&&e.n.n){++r;f(a,0)}do{e=e.n;if(e.w){--o}var n=e.f;e.f=null;n()}while(e.n)}t=true}function p(e,i){if(t&&++o>r){++r;f(a,0)}n=n.n={f:e,w:t,n:null};t=i===true}function v(e){return e==="object"||e==="function"}function m(e){return e==="function"}if(v(typeof process)&&process&&m(typeof process.nextTick)){f=process.nextTick}else if(u&&m(typeof u.setImmediate)){f=function(e){u.setImmediate(e)}}else if(m(typeof MessageChannel)){i=new MessageChannel;i.port1.onmessage=a;f=function(){i.port2.postMessage(0)}}else{f=setTimeout;if(u&&v(typeof Image)&&Image){(function(){var e=0;var n=function(e){var n=new Image;n.onerror=e;n.src="data:image/png,"};try{n(function(){if(--e===0){f=n}});++e}catch(t){}e&&setTimeout(function(){e=0},0)})()}}l=Array.isArray||function(e){return!!e&&c.call(e)==="[object Array]"};function d(e,n){for(var t=0,r=e.length;t<r;++t){if(t in e){n(e[t],t)}}}function h(e,n){if(l(e)){d(e,n);return}for(var t in e){n(e[t],t)}}function y(e){if(e instanceof g){return e}var n=w();n.resolve(e);return n.promise}y.defer=w;function w(){var e=[],n=false,t=new g(o),r;function o(t,o,i,f){var u=i===s?void 0:w();function c(){var e=n?o:t;if(!u){e&&e(r);return}if(typeof e==="function"){try{var i=e(r)}catch(f){u.reject(f);return}u.resolve(i)}else if(n){u.reject(r)}else{u.fulfill(r)}}if(e){e.push(c)}else if(!u&&f){c()}else{p(c)}return u&&u.promise}function i(n){if(e){if(n instanceof g){n.then(f,u,s,true)}else if(n&&typeof n.then==="function"){p(function(){try{n.then(f,u)}catch(e){u(e)}})}else{f(n)}}}function f(o){if(e){t.state=n?"rejected":"fulfilled";t.value=r=o;d(e,p);e=null}}function u(t){if(e){n=true;f(t)}}return{promise:t,resolve:i,fulfill:f,reject:u}}function g(e){this.then=e;this.state="pending";this.value=void 0}g.prototype.done=function(e,n){var t=this;if(e||n){t=t.then(e,n)}t.then(null,function(e){p(function(){if(y.onerror){y.onerror(e)}else{throw e}},true)},s)};g.prototype.spread=function(e,n){return this.then(e&&function(n){return e.apply(void 0,n)},n)};g.prototype.timeout=function(e){var n=w();var t=setTimeout(function(){n.reject(new Error("Timed out after "+e+" ms"))},e);this.when(function(e){clearTimeout(t);n.fulfill(e)},function(e){clearTimeout(t);n.reject(e)},s,true);return n.promise};g.prototype.delay=function(e){var n=this;var t=w();setTimeout(function(){t.resolve(n)},e);return t.promise};y.all=T;function T(e){var n=0;var t=w();h(e,function(r,o){++n;y(r).then(function(r){e[o]=r;if(--n===0){t.fulfill(e)}},t.reject,s)});if(n===0){t.fulfill(e)}return t.promise}y.allSettled=y.allResolved=j;function j(e){var n=1;var t=w();function r(){if(--n===0){t.fulfill(e)}}h(e,function(t,o){++n;e[o]=t=y(t);t.then(r,r,s)});r();return t.promise}y.onerror=null;y.prototype=g.prototype;y.nextTick=function(e){p(e,true)};y.ALT=s;y._each=h;return y}); | ||
(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,w:false,n:null},n=e,t=true,r=0,o=0,i,f,u=v(typeof window)&&window||v(typeof worker)&&worker,c=e.toString,s,a={};function l(){--r;if(e.n){if(!r&&e.n.n){++r;f(l,0)}do{e=e.n;if(e.w){--o}var n=e.f;e.f=null;n()}while(e.n)}t=true}var p=function(e,i){if(t&&++o>r){++r;f(l,0)}n=n.n={f:e,w:t,n:null};t=i===true};function v(e){return e==="object"||e==="function"}function m(e){return e==="function"}if(v(typeof process)&&process&&m(typeof process.nextTick)){p=process.nextTick}else if(u&&m(typeof u.setImmediate)){f=function(e){u.setImmediate(e)}}else if(m(typeof MessageChannel)){i=new MessageChannel;i.port1.onmessage=l;f=function(){i.port2.postMessage(0)}}else{f=setTimeout;if(u&&v(typeof Image)&&Image){(function(){var e=0;var n=function(e){var n=new Image;n.onerror=e;n.src="data:image/png,"};try{n(function(){if(--e===0){f=n}});++e}catch(t){}e&&setTimeout(function(){e=0},0)})()}}function y(e){p(function(){if(w.onerror){w.onerror(e)}else{throw e}},true)}s=Array.isArray||function(e){return!!e&&c.call(e)==="[object Array]"};function d(e,n){for(var t=0,r=e.length;t<r;++t){if(t in e){n(e[t],t)}}}function h(e,n){if(s(e)){d(e,n);return}for(var t in e){n(e[t],t)}}function w(e){if(e instanceof T){return e}var n=g();n.resolve(e);return n.promise}w.defer=g;function g(){var e=[],n=false,t;function r(r,o,i,f){var u=i===a?void 0:g();function c(){var e=n?r:o;if(typeof e==="function"){try{var i=e(t)}catch(f){u?u.reject(f):y(f);return}u&&u.resolve(i)}else if(u){u.resolve(t,a,n)}else if(!n){y(t)}}if(e){e.push(c)}else if(!u&&f){c()}else{p(c)}return u&&u.promise}function o(r,u,c){if(e){if(u===a){n=!!c;t=r;d(e,p);e=null}else if(r instanceof T){r.then(i,f,a,true);return}var s,l=typeof r;if(l==="object"&&r!==null||l==="function"){try{s=r.then;if(typeof s==="function"){s.call(r,o,f);return}}catch(v){f(v);return}}i(r)}}function i(e){o(e,a,true)}function f(e){o(e,a,false)}return{promise:new T(r),resolve:o,reject:f}}function T(e){this.then=e}T.prototype.done=function(e,n){this.then(e,n,a)};T.prototype.spread=function(e,n){return this.then(e&&function(n){return e.apply(void 0,n)},n)};T.prototype.timeout=function(e){var n=g();var t=setTimeout(function(){n.reject(new Error("Timed out after "+e+" ms"))},e);this.then(function(e){clearTimeout(t);n.resolve(e)},function(e){clearTimeout(t);n.reject(e)},a,true);return n.promise};T.prototype.delay=function(e){var n=this;var t=g();setTimeout(function(){t.resolve(n)},e);return t.promise};w.all=j;function j(e){var n=0;var t=g();h(e,function(r,o){++n;w(r).then(function(r){e[o]=r;if(--n===0){t.resolve(e)}},t.reject,a)});if(n===0){t.resolve(e)}return t.promise}w.onerror=null;w.prototype=T.prototype;w.nextTick=function(e){p(e,true)};w._each=h;return w}); |
{ | ||
"name": "p-promise", | ||
"version": "0.0.14", | ||
"version": "0.1.0", | ||
"description": "A simple Promises/A+ library.", | ||
@@ -5,0 +5,0 @@ "author": "Robert Katić <robert.katic@gmail.com> (https://github.com/rkatic)", |
@@ -19,4 +19,2 @@ [![Build Status](https://secure.travis-ci.org/rkatic/p.png)](http://travis-ci.org/rkatic/p) | ||
- `P.all(promises)` (array **or object** of promises) | ||
- `P.allSettled(promises)` (array **or object** of promises) | ||
- `P.allResolved(promises)` **DEPRECATED - use P.allSettled instead** | ||
- `P.onerror` | ||
@@ -26,3 +24,2 @@ - `P.nextTick(callback)` | ||
- `deferred.resolve(value)` | ||
- `deferred.fulfill(value)` | ||
- `deferred.reject(reason)` | ||
@@ -34,3 +31,1 @@ - `promise.then(onFulfilled, onRejected)` | ||
- `promise.delay(ms)` | ||
- **`promise.state` ("pending", "fulfilled, or "rejected")** | ||
- **`promise.value` (resolved value/exception)** |
var P = require('../p'); | ||
exports.pending = P.defer; | ||
exports.pending = function() { | ||
var d = P.defer(); | ||
return { | ||
promise: d.promise, | ||
fulfill: d.resolve, | ||
reject: d.reject | ||
}; | ||
}; |
15108
425
29