Comparing version 0.9.0 to 0.10.0
{ | ||
"name" : "ya-promise" | ||
, "version" : "0.9.0" | ||
, "version" : "0.10.0" | ||
, "description" : "Yet Another promise/A+ library" | ||
@@ -5,0 +5,0 @@ , "keywords" : ["defer", "deferred", "promise", "promise/A", "promise/A+" |
@@ -147,2 +147,40 @@ # Yet Another Promise/A+ Library | ||
### Create a Promise from an Array of Promises | ||
```javascript | ||
Y.all([promA, promB, promC]).then( function([resA, resB, resC]){ ... } | ||
, function(reason){ ... } ) | ||
``` | ||
When all the promises in the array passed to `Y.all(array)` are resolved | ||
the returned promise is resolved. It value is an array of the results of | ||
each of the original promises in the same order. | ||
If ANY of the promises in the array are rejected then the returned promise | ||
is immediately rejected. | ||
Example: | ||
```javascript | ||
var Y = require('./') | ||
function timeout(n) { | ||
var d = Y.defer(), t = n * Math.random() | ||
setTimeout(function(){ d.resolve(t) }, t*1000 ) | ||
return d.promise | ||
} | ||
var t0 = Date.now() | ||
Y.all([ timeout(10, "one") | ||
, timeout(10, "two") | ||
, timeout(10, "three") | ||
]) | ||
.then(function(a){ | ||
a.forEach(function(r, i){ | ||
console.log("%d: %d sec", i, r) | ||
}) | ||
console.log("now-t0: %d sec", (Date.now()-t0)/1000) | ||
}) | ||
``` | ||
### Create a Fulfilled or Rejected Promise | ||
@@ -154,3 +192,3 @@ | ||
``` | ||
examples: | ||
Examples: | ||
```javascript | ||
@@ -157,0 +195,0 @@ Y.reolved(42).then( function(value){ value == 42 } |
@@ -75,2 +75,13 @@ | ||
Y.all = function(promises){ | ||
var n = 0, d = Y.defer(), res = [] | ||
promises.forEach(function(p, i){ | ||
n += 1 | ||
Y.when(p).then( | ||
function(v){ n -= 1; res[i] = v; if (n === 0) d.resolve(res) } | ||
, function(r){ d.reject(r) }) | ||
}) | ||
return d.promise | ||
} | ||
Y.promisify = promisify | ||
@@ -139,3 +150,2 @@ | ||
execute(q[i].fulfilled, value, q[i].deferred, q[i].spread) | ||
//execute(q[i].fulfilled, value, q[i].deferred, true) | ||
} | ||
@@ -145,3 +155,4 @@ | ||
promise.then = createFulfilled(promise, value) | ||
promise.then = createFulfilled(promise, value, false) | ||
promise.spread = createFulfilled(promise, value, true) | ||
} | ||
@@ -160,2 +171,3 @@ | ||
promise.then = createRejected(promise, reason) | ||
promise.spread = createRejected(promise, reason) | ||
} | ||
@@ -171,7 +183,8 @@ | ||
function createFulfilled(promise, value) { | ||
function createFulfilled(promise, value, spread) { | ||
spread = spread || false | ||
return function fulfilled(onFulfilled, onRejected){ | ||
var d | ||
if (typeof onFulfilled !== 'function') return promise | ||
execute(onFulfilled, value, d = new Deferred()) | ||
execute(onFulfilled, value, d = new Deferred(), spread) | ||
return d.promise | ||
@@ -185,3 +198,3 @@ } | ||
if (typeof onRejected !== 'function') return promise | ||
execute(onRejected, reason, d = new Deferred()) | ||
execute(onRejected, reason, d = new Deferred(), false) | ||
return d.promise | ||
@@ -201,4 +214,6 @@ } | ||
result = callback(value) | ||
if (result && typeof result.then == 'function') | ||
result.then(deferred.resolve, deferred.reject) | ||
result.then( function(v) { deferred.resolve(v) } | ||
, function(r) { deferred.reject(r) } ) | ||
else | ||
@@ -205,0 +220,0 @@ deferred.resolve(result) |
@@ -1,1 +0,1 @@ | ||
(function(e){if(typeof bootstrap==="function"){bootstrap("promise",e)}else if(typeof exports==="object"){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else if(typeof ses!=="undefined"){if(!ses.ok()){return}else{ses.makeY=e}}else{Y=e()}})(function(){"use strict";function e(r){return e.when(r)}e.defer=function(){return new i};e.deferred=e.defer;e.resolved=function(e){var r=new i;r.resolve(e);return r.promise};e.rejected=function(e){var r=new i;r.reject(e);return r.promise};e.when=function(e){if(r(e))return e;var n=new i;if(e&&typeof e.then==="function")e.then(function(e){n.resolve(e)},function(e){n.reject(e)});else n.resolve(e);return n.promise};e.promisify=l;e.nfbind=l;e.isPromise=function(e){return e instanceof f};e.isDeferred=function(e){return e instanceof i};var r=e.isPromise,n=e.isDeferred;var t;Object.defineProperty(e,"nextTick",{get:function(){return t},set:function(e){t=e;return e},configurable:true,enumerable:true});if(typeof setImmediate==="function"){if(typeof window!=="undefined")e.nextTick=setImmediate.bind(window);else e.nextTick=setImmediate}else if(typeof process!=="undefined"&&process.nextTick)e.nextTick=process.nextTick;else e.nextTick=function(e){setTimeout(e,0)};function i(){var e=[],r=new f(t,l),n={promise:r,resolve:d,reject:p};function t(r,n){var t=new i;e.push({deferred:t,fulfilled:r,rejected:n,spread:false});return t.promise}function l(r,n){var t=new i;e.push({deferred:t,fulfilled:r,rejected:n,spread:true});return t.promise}function d(t){for(var i=0;i<e.length;i++){if(typeof e[i].fulfilled!=="function")e[i].deferred.resolve(t);else c(e[i].fulfilled,t,e[i].deferred,e[i].spread)}n.reject=n.resolve=u;r.then=o(r,t)}function p(t){for(var i=0;i<e.length;i++){if(typeof e[i].rejected!=="function")e[i].deferred.reject(t);else c(e[i].rejected,t,e[i].deferred,false)}n.reject=n.resolve=u;r.then=s(r,t)}return n}function f(e,r){this.then=e;this.spread=r}function o(e,r){return function n(n,t){var f;if(typeof n!=="function")return e;c(n,r,f=new i);return f.promise}}function s(e,r){return function n(n,t){var f;if(typeof t!=="function")return e;c(t,r,f=new i);return f.promise}}function u(){}function c(e,r,n,i){t(function(){var t;try{if(i&&Array.isArray(r))t=e.apply(void 0,r);else t=e(r);if(t&&typeof t.then=="function")t.then(n.resolve,n.reject);else n.resolve(t)}catch(f){n.reject(f)}})}function l(r,n){return function(){var t=Array.prototype.slice.call(arguments),i=e.defer();t.push(function(e){if(e){i.reject(e);return}if(arguments.length>2)i.resolve(Array.prototype.slice.call(arguments,1));else i.resolve(arguments[1])});r.apply(n,t);return i.promise}}return e}); | ||
(function(e){if(typeof bootstrap==="function"){bootstrap("promise",e)}else if(typeof exports==="object"){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else if(typeof ses!=="undefined"){if(!ses.ok()){return}else{ses.makeY=e}}else{Y=e()}})(function(){"use strict";function e(n){return e.when(n)}e.defer=function(){return new i};e.deferred=e.defer;e.resolved=function(e){var n=new i;n.resolve(e);return n.promise};e.rejected=function(e){var n=new i;n.reject(e);return n.promise};e.when=function(e){if(n(e))return e;var r=new i;if(e&&typeof e.then==="function")e.then(function(e){r.resolve(e)},function(e){r.reject(e)});else r.resolve(e);return r.promise};e.all=function(n){var r=0,t=e.defer(),i=[];n.forEach(function(n,f){r+=1;e.when(n).then(function(e){r-=1;i[f]=e;if(r===0)t.resolve(i)},function(e){t.reject(e)})});return t.promise};e.promisify=l;e.nfbind=l;e.isPromise=function(e){return e instanceof f};e.isDeferred=function(e){return e instanceof i};var n=e.isPromise,r=e.isDeferred;var t;Object.defineProperty(e,"nextTick",{get:function(){return t},set:function(e){t=e;return e},configurable:true,enumerable:true});if(typeof setImmediate==="function"){if(typeof window!=="undefined")e.nextTick=setImmediate.bind(window);else e.nextTick=setImmediate}else if(typeof process!=="undefined"&&process.nextTick)e.nextTick=process.nextTick;else e.nextTick=function(e){setTimeout(e,0)};function i(){var e=[],n=new f(t,l),r={promise:n,resolve:d,reject:a};function t(n,r){var t=new i;e.push({deferred:t,fulfilled:n,rejected:r,spread:false});return t.promise}function l(n,r){var t=new i;e.push({deferred:t,fulfilled:n,rejected:r,spread:true});return t.promise}function d(t){for(var i=0;i<e.length;i++){if(typeof e[i].fulfilled!=="function")e[i].deferred.resolve(t);else c(e[i].fulfilled,t,e[i].deferred,e[i].spread)}r.reject=r.resolve=u;n.then=o(n,t,false);n.spread=o(n,t,true)}function a(t){for(var i=0;i<e.length;i++){if(typeof e[i].rejected!=="function")e[i].deferred.reject(t);else c(e[i].rejected,t,e[i].deferred,false)}r.reject=r.resolve=u;n.then=s(n,t);n.spread=s(n,t)}return r}function f(e,n){this.then=e;this.spread=n}function o(e,n,r){r=r||false;return function t(t,f){var o;if(typeof t!=="function")return e;c(t,n,o=new i,r);return o.promise}}function s(e,n){return function r(r,t){var f;if(typeof t!=="function")return e;c(t,n,f=new i,false);return f.promise}}function u(){}function c(e,n,r,i){t(function(){var t;try{if(i&&Array.isArray(n))t=e.apply(void 0,n);else t=e(n);if(t&&typeof t.then=="function")t.then(function(e){r.resolve(e)},function(e){r.reject(e)});else r.resolve(t)}catch(f){r.reject(f)}})}function l(n,r){return function(){var t=Array.prototype.slice.call(arguments),i=e.defer();t.push(function(e){if(e){i.reject(e);return}if(arguments.length>2)i.resolve(Array.prototype.slice.call(arguments,1));else i.resolve(arguments[1])});n.apply(r,t);return i.promise}}return e}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25714
208
544