Comparing version 0.0.4 to 0.0.5
122
p.js
@@ -108,9 +108,18 @@ ;(function( factory ){ | ||
for ( var i = 0, l = arr.length; i < l; ++i ) { | ||
cb( arr[i], i ); | ||
if ( i in arr ) { | ||
cb( arr[i], i ); | ||
} | ||
} | ||
} | ||
var P = resolve; | ||
P.prototype = Promise.prototype; | ||
function P( val ) { | ||
if ( val instanceof Promise ) { | ||
return val; | ||
} | ||
var def = defer(); | ||
def.resolve( val ); | ||
return def.promise; | ||
} | ||
P.defer = defer; | ||
@@ -120,2 +129,3 @@ function defer() { | ||
rejected = false, | ||
promise = new Promise( then ), | ||
value; | ||
@@ -129,18 +139,5 @@ | ||
if ( typeof func !== "function" ) { | ||
if ( rejected ) { | ||
def.reject( value ); | ||
} else { | ||
def.fulfill( value ); | ||
} | ||
} else { | ||
var val; | ||
if ( typeof func === "function" ) { | ||
try { | ||
val = func( value ); | ||
if ( val && typeof val.then === "function" ) { | ||
val.then( def.fulfill, def.reject ); | ||
return; | ||
} | ||
def.resolve( func( value ) ); | ||
} catch ( ex ) { | ||
@@ -150,3 +147,7 @@ def.reject( ex ); | ||
def.fulfill( val ); | ||
} else if ( rejected ) { | ||
def.reject( value ); | ||
} else { | ||
def.fulfill( value ); | ||
} | ||
@@ -165,5 +166,17 @@ } | ||
function resolve( val ) { | ||
if ( pending ) { | ||
if ( val && typeof val.then === "function" ) { | ||
val.then( fulfill, reject ); | ||
} else { | ||
fulfill( val ); | ||
} | ||
} | ||
} | ||
function fulfill( val ) { | ||
if ( pending ) { | ||
value = val; | ||
promise.state = rejected ? "rejected" : "fulfilled"; | ||
promise.value = value = val; | ||
each( pending, runLater ); | ||
@@ -182,3 +195,4 @@ pending = null; | ||
return { | ||
promise: new Promise( then ), | ||
promise: promise, | ||
resolve: resolve, | ||
fulfill: fulfill, | ||
@@ -192,2 +206,4 @@ reject: reject | ||
this.then = then; | ||
this.state = "pending"; | ||
this.value = void 0; | ||
} | ||
@@ -217,27 +233,2 @@ | ||
P.resolve = resolve; | ||
function resolve( val ) { | ||
if ( val instanceof Promise ) { | ||
return val; | ||
} | ||
var def = defer(); | ||
if ( val && typeof val.then === "function" ) { | ||
val.then( def.fulfill, def.reject ); | ||
} else { | ||
def.fulfill( val ); | ||
} | ||
return def.promise; | ||
} | ||
P.reject = reject; | ||
function reject( value ) { | ||
var def = defer(); | ||
def.reject( value ); | ||
return def.promise; | ||
} | ||
P.all = all; | ||
@@ -248,11 +239,9 @@ function all( promises ) { | ||
each(promises, function( promise, index ) { | ||
if ( index in promises ) { | ||
++waiting; | ||
resolve( promise ).then(function( value ) { | ||
promises[ index ] = value; | ||
if ( --waiting === 0 ) { | ||
def.fulfill( promises ); | ||
} | ||
}, def.reject ); | ||
} | ||
++waiting; | ||
P( promise ).then(function( value ) { | ||
promises[ index ] = value; | ||
if ( --waiting === 0 ) { | ||
def.fulfill( promises ); | ||
} | ||
}, def.reject ); | ||
}); | ||
@@ -265,16 +254,23 @@ if ( waiting === 0 ) { | ||
P.promise = function( makeOrPromise ) { | ||
P.allResolved = allResolved; | ||
function allResolved( promise ) { | ||
var waiting = 1; | ||
var def = defer(); | ||
resolve( makeOrPromise ).then(function( make ) { | ||
try { | ||
make( def.fulfill, def.reject ); | ||
} catch ( ex ) { | ||
def.reject( ex ); | ||
function callback() { | ||
if ( --waiting === 0 ) { | ||
def.fulfill( promise ); | ||
} | ||
}, def.reject); | ||
} | ||
each(promise, function( promise ) { | ||
++waiting; | ||
P( promise ).then( callback, callback ); | ||
}); | ||
callback(); | ||
return def.promise; | ||
}; | ||
} | ||
P.onerror = null; | ||
P.prototype = Promise.prototype; | ||
P.nextTick = function( f ) { | ||
@@ -281,0 +277,0 @@ runLater( f, true ); |
@@ -1,1 +0,1 @@ | ||
(function(e){if(typeof module!=="undefined"&&module&&module.exports){module.exports=e()}else if(typeof define!=="function"){define(e)}else{P=e()}})(function(){"use strict";var e={f:null,w:false,n:null},n=e,t=true,r=0,f=0,o,i,u=s(typeof window)&&window||s(typeof worker)&&worker;function c(){--r;while(e.n){e=e.n;if(e.w){--f}var n=e.f;e.f=null;n()}t=true}function l(e,o){if(t&&++f>r){++r;i(c,0)}n=n.n={f:e,w:t,n:null};t=o===true}function s(e){return e==="object"||e==="function"}function p(e){return e==="function"}if(s(typeof process)&&process&&p(typeof process.nextTick)){i=process.nextTick}else if(u&&p(typeof u.setImmediate)){i=function(e){u.setImmediate(e)}}else if(p(typeof MessageChannel)){o=new MessageChannel;o.port1.onmessage=c;i=function(){o.port2.postMessage(0)}}else{i=setTimeout;if(u&&s(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){i=n}});++e}catch(t){}e&&setTimeout(function(){e=0},0)})()}}function a(e,n){for(var t=0,r=e.length;t<r;++t){n(e[t],t)}}var h=v;h.prototype=y.prototype;h.defer=m;function m(){var e=[],n=false,t;function r(r,f){var o=m();function i(){var e=n?f:r;if(typeof e!=="function"){if(n){o.reject(t)}else{o.fulfill(t)}}else{var i;try{i=e(t);if(i&&typeof i.then==="function"){i.then(o.fulfill,o.reject);return}}catch(u){o.reject(u)}o.fulfill(i)}}if(e){e.push(i)}else{l(i)}return o.promise}function f(n){if(e){t=n;a(e,l);e=null}}function o(t){if(e){n=true;f(t)}}return{promise:new y(r),fulfill:f,reject:o}}function y(e){this.then=e}y.prototype.done=function(e,n){var t=this;if(e||n){t=t.then(e,n)}t.then(null,function(e){l(function(){if(h.onerror){h.onerror(e)}else{throw e}},true)})};y.prototype.spread=function(e,n){return this.then(e&&function(n){return e.apply(void 0,n)},n)};h.resolve=v;function v(e){if(e instanceof y){return e}var n=m();if(e&&typeof e.then==="function"){e.then(n.fulfill,n.reject)}else{n.fulfill(e)}return n.promise}h.reject=d;function d(e){var n=m();n.reject(e);return n.promise}h.all=w;function w(e){var n=0;var t=m();a(e,function(r,f){if(f in e){++n;v(r).then(function(r){e[f]=r;if(--n===0){t.fulfill(e)}},t.reject)}});if(n===0){t.fulfill(e)}return t.promise}h.promise=function(e){var n=m();v(e).then(function(e){try{e(n.fulfill,n.reject)}catch(t){n.reject(t)}},n.reject);return n.promise};h.onerror=null;h.nextTick=function(e){l(e,true)};return h}); | ||
(function(e){if(typeof module!=="undefined"&&module&&module.exports){module.exports=e()}else if(typeof define!=="function"){define(e)}else{P=e()}})(function(){"use strict";var e={f:null,w:false,n:null},n=e,t=true,o=0,i=0,f,r,u=s(typeof window)&&window||s(typeof worker)&&worker;function l(){--o;while(e.n){e=e.n;if(e.w){--i}var n=e.f;e.f=null;n()}t=true}function c(e,f){if(t&&++i>o){++o;r(l,0)}n=n.n={f:e,w:t,n:null};t=f===true}function s(e){return e==="object"||e==="function"}function a(e){return e==="function"}if(s(typeof process)&&process&&a(typeof process.nextTick)){r=process.nextTick}else if(u&&a(typeof u.setImmediate)){r=function(e){u.setImmediate(e)}}else if(a(typeof MessageChannel)){f=new MessageChannel;f.port1.onmessage=l;r=function(){f.port2.postMessage(0)}}else{r=setTimeout;if(u&&s(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){r=n}});++e}catch(t){}e&&setTimeout(function(){e=0},0)})()}}function p(e,n){for(var t=0,o=e.length;t<o;++t){if(t in e){n(e[t],t)}}}function v(e){if(e instanceof h){return e}var n=d();n.resolve(e);return n.promise}v.defer=d;function d(){var e=[],n=false,t=new h(i),o;function i(t,i){var f=d();function r(){var e=n?i:t;if(typeof e==="function"){try{f.resolve(e(o))}catch(r){f.reject(r)}}else if(n){f.reject(o)}else{f.fulfill(o)}}if(e){e.push(r)}else{c(r)}return f.promise}function f(n){if(e){if(n&&typeof n.then==="function"){n.then(r,u)}else{r(n)}}}function r(i){if(e){t.state=n?"rejected":"fulfilled";t.value=o=i;p(e,c);e=null}}function u(t){if(e){n=true;r(t)}}return{promise:t,resolve:f,fulfill:r,reject:u}}function h(e){this.then=e;this.state="pending";this.value=void 0}h.prototype.done=function(e,n){var t=this;if(e||n){t=t.then(e,n)}t.then(null,function(e){c(function(){if(v.onerror){v.onerror(e)}else{throw e}},true)})};h.prototype.spread=function(e,n){return this.then(e&&function(n){return e.apply(void 0,n)},n)};v.all=m;function m(e){var n=0;var t=d();p(e,function(o,i){++n;v(o).then(function(o){e[i]=o;if(--n===0){t.fulfill(e)}},t.reject)});if(n===0){t.fulfill(e)}return t.promise}v.allResolved=y;function y(e){var n=1;var t=d();function o(){if(--n===0){t.fulfill(e)}}p(e,function(e){++n;v(e).then(o,o)});o();return t.promise}v.onerror=null;v.prototype=h.prototype;v.nextTick=function(e){c(e,true)};return v}); |
{ | ||
"name": "p-promise", | ||
"version": "0.0.4", | ||
"description": "Simple promise-a library.", | ||
"version": "0.0.5", | ||
"description": "A simple Promises/A+ library.", | ||
"author": "Robert Katić <robert.katic@gmail.com> (https://github.com/rkatic)", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/rkatic/p.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/rkatic/p/issues" | ||
}, | ||
"main": "p.js", | ||
"scripts": { | ||
"pretest": "uglifyjs p.js -o p.min.js -m", | ||
"test": "promises-aplus-tests test/adapter && promises-aplus-tests test/adapter-min" | ||
"posttest": "uglifyjs p.js -o p.min.js -m", | ||
"test": "promises-aplus-tests test/adapter" | ||
}, | ||
@@ -14,4 +23,2 @@ "keywords": [ | ||
], | ||
"author": "rkatic", | ||
"license": "MIT", | ||
"devDependencies": { | ||
@@ -18,0 +25,0 @@ "promises-aplus-tests": "*", |
@@ -7,3 +7,3 @@ [![Build Status](https://secure.travis-ci.org/rkatic/p.png)](http://travis-ci.org/rkatic/p) | ||
- A subset of the [Q](https://github.com/kriskowal/q) library. | ||
- Implements a subset of the the [Q](https://github.com/kriskowal/q) API. | ||
- Passing the [Promises/A+ Compliance Test Suite](https://github.com/promises-aplus/promises-tests). | ||
@@ -15,11 +15,12 @@ - Small. | ||
P is a subset of [Q](https://github.com/kriskowal/q). | ||
P implements a subset of the [Q](https://github.com/kriskowal/q) API. | ||
- `P(val)` same as `P.resolve(val)` | ||
- `P.reject(reason)` | ||
- `P(val)` | ||
- `P.defer()` | ||
- `P.all(promises)` | ||
- `P.allResolved(promises)` | ||
- `P.onerror` | ||
- `P.nextTick(callback)` | ||
- `deferred.promise` | ||
- `deferred.resolve(value)` | ||
- `deferred.fulfill(value)` | ||
@@ -30,1 +31,3 @@ - `deferred.reject(reason)` | ||
- `promise.spread(onFulfilled, onRejected)` | ||
- `promise.state` ("pending", "fulfilled, or "rejected") | ||
- `promise.value` (resolved value/exception) |
@@ -5,3 +5,1 @@ | ||
exports.pending = P.defer; | ||
exports.fulfilled = P.resolve; | ||
exports.rejected = P.reject; |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
9877
1
31
0
8
244