Comparing version 3.3.1 to 3.4.0
@@ -9,9 +9,6 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var unhandledRejections = require('../lib/decorators/unhandledRejection'); | ||
var PromiseConstructor = module.exports = unhandledRejections(require('../lib/Promise')); | ||
var PromiseConstructor = unhandledRejections(require('../lib/Promise')); | ||
var g = typeof global !== 'undefined' && global | ||
|| typeof self !== 'undefined' && self; | ||
if(typeof g !== 'undefined' && typeof g.Promise === 'undefined') { | ||
g['Promise'] = PromiseConstructor; | ||
} | ||
module.exports = typeof global != 'undefined' ? (global.Promise = PromiseConstructor) | ||
: typeof self != 'undefined' ? (self.Promise = PromiseConstructor) | ||
: PromiseConstructor; |
@@ -10,11 +10,8 @@ !function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.Promise=e():"undefined"!=typeof global?global.Promise=e():"undefined"!=typeof self&&(self.Promise=e())}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var unhandledRejections = require('../lib/decorators/unhandledRejection'); | ||
var PromiseConstructor = module.exports = unhandledRejections(require('../lib/Promise')); | ||
var PromiseConstructor = unhandledRejections(require('../lib/Promise')); | ||
var g = typeof global !== 'undefined' && global | ||
|| typeof self !== 'undefined' && self; | ||
module.exports = typeof global != 'undefined' ? (global.Promise = PromiseConstructor) | ||
: typeof self != 'undefined' ? (self.Promise = PromiseConstructor) | ||
: PromiseConstructor; | ||
if(typeof g !== 'undefined' && typeof g.Promise === 'undefined') { | ||
g['Promise'] = PromiseConstructor; | ||
} | ||
},{"../lib/Promise":2,"../lib/decorators/unhandledRejection":6}],2:[function(require,module,exports){ | ||
@@ -519,13 +516,2 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
/** | ||
* Private function to bind a thisArg for this promise's handlers | ||
* @private | ||
* @param {object} thisArg `this` value for all handlers attached to | ||
* the returned promise. | ||
* @returns {Promise} | ||
*/ | ||
Promise.prototype._bindContext = function(thisArg) { | ||
return new Promise(Handler, new Bound(this._handler, thisArg)); | ||
}; | ||
/** | ||
* Creates a new, pending promise of the same type as this promise | ||
@@ -682,4 +668,2 @@ * @private | ||
Handler.prototype.inspect = toPendingState; | ||
Handler.prototype._state = 0; | ||
@@ -755,6 +739,2 @@ | ||
Pending.prototype.inspect = function() { | ||
return this.resolved ? this.join().inspect() : toPendingState(); | ||
}; | ||
Pending.prototype.resolve = function(x) { | ||
@@ -857,6 +837,2 @@ this.become(getHandler(x)); | ||
Delegating.prototype.inspect = function() { | ||
return this.join().inspect(); | ||
}; | ||
Delegating.prototype._report = function(context) { | ||
@@ -886,26 +862,2 @@ this.join()._report(context); | ||
/** | ||
* Handler that follows another handler, injecting a receiver | ||
* @param {object} handler another handler to follow | ||
* @param {object=undefined} receiver | ||
* @constructor | ||
*/ | ||
function Bound(handler, receiver) { | ||
Delegating.call(this, handler); | ||
this.receiver = receiver; | ||
} | ||
inherit(Delegating, Bound); | ||
Bound.prototype.when = function(continuation) { | ||
// Because handlers are allowed to be shared among promises, | ||
// each of which possibly having a different receiver, we have | ||
// to insert our own receiver into the chain if it has been set | ||
// so that callbacks (f, r, u) will be called using our receiver | ||
if(this.receiver !== void 0) { | ||
continuation.receiver = this.receiver; | ||
} | ||
this.join().when(continuation); | ||
}; | ||
/** | ||
* Handler that wraps an untrusted thenable and assimilates it in a future stack | ||
@@ -937,6 +889,2 @@ * @param {function} then | ||
Fulfilled.prototype.inspect = function() { | ||
return { state: 'fulfilled', value: this.value }; | ||
}; | ||
Fulfilled.prototype.fold = function(f, z, c, to) { | ||
@@ -972,6 +920,2 @@ runContinuation3(f, z, this, c, to); | ||
Rejected.prototype.inspect = function() { | ||
return { state: 'rejected', reason: this.value }; | ||
}; | ||
Rejected.prototype.fold = function(f, z, c, to) { | ||
@@ -1043,12 +987,2 @@ this._unreport(); | ||
// Snapshot states | ||
/** | ||
* Creates a pending state snapshot | ||
* @returns {{state:'pending'}} | ||
*/ | ||
function toPendingState() { | ||
return { state: 'pending' }; | ||
} | ||
// Task runners | ||
@@ -1055,0 +989,0 @@ |
48
keys.js
@@ -14,2 +14,3 @@ /** @license MIT License (c) copyright 2011-2013 original author or authors */ | ||
var when = require('./when'); | ||
var Promise = when.Promise; | ||
var toPromise = when.resolve; | ||
@@ -30,24 +31,27 @@ | ||
function all(object) { | ||
return when.promise(function(resolve, reject, notify) { | ||
var results = {}; | ||
var pending = 0; | ||
var p = Promise._defer(); | ||
var resolver = Promise._handler(p); | ||
for(var k in object) { | ||
++pending; | ||
resolveOne(object[k], k); | ||
} | ||
var results = {}; | ||
var keys = Object.keys(object); | ||
var pending = keys.length; | ||
if(pending === 0) { | ||
resolve(results); | ||
} | ||
for(var i=0, k; i<keys.length; ++i) { | ||
k = keys[i]; | ||
Promise._handler(object[k]).fold(settleKey, k, results, resolver); | ||
} | ||
function resolveOne(x, k) { | ||
toPromise(x).then(function(x) { | ||
results[k] = x; | ||
if(--pending === 0) { | ||
resolve(results); | ||
} | ||
}, reject, notify); | ||
if(pending === 0) { | ||
resolver.resolve(results); | ||
} | ||
return p; | ||
function settleKey(k, x, resolver) { | ||
/*jshint validthis:true*/ | ||
this[k] = x; | ||
if(--pending === 0) { | ||
resolver.resolve(results); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -59,3 +63,3 @@ | ||
* will be reduced | ||
* @param {function} f mapping function mapFunc(value) which may | ||
* @param {function(value:*, key:String):*} f mapping function which may | ||
* return either a promise or a value | ||
@@ -68,6 +72,10 @@ * @returns {Promise} promise for an object with the mapped and fully | ||
return all(Object.keys(object).reduce(function(o, k) { | ||
o[k] = toPromise(object[k]).then(f); | ||
o[k] = toPromise(object[k]).fold(mapWithKey, k); | ||
return o; | ||
}, {})); | ||
}); | ||
function mapWithKey(k, x) { | ||
return f(x, k); | ||
} | ||
} | ||
@@ -74,0 +82,0 @@ |
@@ -10,6 +10,4 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var arrayMap = Array.prototype.map; | ||
var arrayReduce = Array.prototype.reduce; | ||
var arrayReduceRight = Array.prototype.reduceRight; | ||
var arrayForEach = Array.prototype.forEach; | ||
@@ -26,2 +24,3 @@ var toPromise = Promise.resolve; | ||
Promise.map = map; | ||
Promise.filter = filter; | ||
Promise.reduce = reduce; | ||
@@ -124,11 +123,15 @@ Promise.reduceRight = reduceRight; | ||
/** | ||
* Initialize a race observing each promise in the input promises | ||
* @param {Array} promises | ||
* @param {function} resolve | ||
* @param {function} reject | ||
* @param {?function=} notify | ||
* @returns {Number} actual count of items being raced | ||
*/ | ||
function initRace(promises, resolve, reject, notify) { | ||
var pending = 0; | ||
arrayForEach.call(promises, function(p) { | ||
++pending; | ||
return arrayReduce.call(promises, function(pending, p) { | ||
toPromise(p).then(resolve, reject, notify); | ||
}); | ||
return pending; | ||
return pending + 1; | ||
}, 0); | ||
} | ||
@@ -140,13 +143,49 @@ | ||
* @param {array} promises | ||
* @param {function} f | ||
* @param {function} fallback | ||
* @param {function(x:*, index:Number):*} f mapping function | ||
* @returns {Promise} | ||
*/ | ||
function map(promises, f, fallback) { | ||
return all(arrayMap.call(promises, function(x) { | ||
return toPromise(x).then(f, fallback); | ||
})); | ||
function map(promises, f) { | ||
if(typeof promises !== 'object') { | ||
return toPromise([]); | ||
} | ||
return all(mapArray(function(x, i) { | ||
return toPromise(x).fold(mapWithIndex, i); | ||
}, promises)); | ||
function mapWithIndex(k, x) { | ||
return f(x, k); | ||
} | ||
} | ||
/** | ||
* Filter the provided array of promises using the provided predicate. Input may | ||
* contain promises and values | ||
* @param {Array} promises array of promises and values | ||
* @param {function(x:*, index:Number):boolean} predicate filtering predicate. | ||
* Must return truthy (or promise for truthy) for items to retain. | ||
* @returns {Promise} promise that will fulfill with an array containing all items | ||
* for which predicate returned truthy. | ||
*/ | ||
function filter(promises, predicate) { | ||
return all(promises).then(function(values) { | ||
return all(mapArray(predicate, values)).then(function(results) { | ||
var len = results.length; | ||
var filtered = new Array(len); | ||
for(var i=0, j= 0, x; i<len; ++i) { | ||
x = results[i]; | ||
if(x === void 0 && !(i in results)) { | ||
continue; | ||
} | ||
if(results[i]) { | ||
filtered[j++] = values[i]; | ||
} | ||
} | ||
filtered.length = j; | ||
return filtered; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Return a promise that will always fulfill with an array containing | ||
@@ -156,6 +195,6 @@ * the outcome states of all input promises. The returned promise | ||
* @param {array} promises | ||
* @returns {Promise} | ||
* @returns {Promise} promise for array of settled state descriptors | ||
*/ | ||
function settle(promises) { | ||
return all(arrayMap.call(promises, function(p) { | ||
return all(mapArray(function(p) { | ||
p = toPromise(p); | ||
@@ -167,5 +206,11 @@ return p.then(inspect, inspect); | ||
} | ||
})); | ||
}, promises)); | ||
} | ||
/** | ||
* Reduce an array of promises and values | ||
* @param {Array} promises | ||
* @param {function(accumulated:*, x:*, index:Number):*} f reduce function | ||
* @returns {Promise} promise for reduced value | ||
*/ | ||
function reduce(promises, f) { | ||
@@ -178,2 +223,8 @@ var reducer = makeReducer(f); | ||
/** | ||
* Reduce an array of promises and values from the right | ||
* @param {Array} promises | ||
* @param {function(accumulated:*, x:*, index:Number):*} f reduce function | ||
* @returns {Promise} promise for reduced value | ||
*/ | ||
function reduceRight(promises, f) { | ||
@@ -195,2 +246,15 @@ var reducer = makeReducer(f); | ||
} | ||
function mapArray(f, a) { | ||
var l = a.length; | ||
var b = new Array(l); | ||
for(var i=0, x; i<l; ++i) { | ||
x = a[i]; | ||
if(x === void 0 && !(i in a)) { | ||
continue; | ||
} | ||
b[i] = f(a[i], i); | ||
} | ||
return b; | ||
} | ||
}; | ||
@@ -197,0 +261,0 @@ |
@@ -8,8 +8,22 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
return function inspect(Promise) { | ||
return function inspection(Promise) { | ||
Promise.prototype.inspect = function() { | ||
return this._handler.inspect(); | ||
return inspect(Promise._handler(this)); | ||
}; | ||
function inspect(handler) { | ||
var state = handler.state(); | ||
if(state === 0) { | ||
return { state: 'pending' }; | ||
} | ||
if(state > 0) { | ||
return { state: 'fulfilled', value: handler.value }; | ||
} | ||
return { state: 'rejected', reason: handler.value }; | ||
} | ||
return Promise; | ||
@@ -16,0 +30,0 @@ }; |
@@ -24,4 +24,9 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
*/ | ||
Promise.prototype['with'] = Promise.prototype.withThis | ||
= Promise.prototype._bindContext; | ||
Promise.prototype['with'] = Promise.prototype.withThis = function(receiver) { | ||
var p = this._beget(); | ||
var child = p._handler; | ||
child.receiver = receiver; | ||
this._handler.chain(child, receiver); | ||
return p; | ||
}; | ||
@@ -28,0 +33,0 @@ return Promise; |
@@ -157,13 +157,2 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
/** | ||
* Private function to bind a thisArg for this promise's handlers | ||
* @private | ||
* @param {object} thisArg `this` value for all handlers attached to | ||
* the returned promise. | ||
* @returns {Promise} | ||
*/ | ||
Promise.prototype._bindContext = function(thisArg) { | ||
return new Promise(Handler, new Bound(this._handler, thisArg)); | ||
}; | ||
/** | ||
* Creates a new, pending promise of the same type as this promise | ||
@@ -320,4 +309,2 @@ * @private | ||
Handler.prototype.inspect = toPendingState; | ||
Handler.prototype._state = 0; | ||
@@ -393,6 +380,2 @@ | ||
Pending.prototype.inspect = function() { | ||
return this.resolved ? this.join().inspect() : toPendingState(); | ||
}; | ||
Pending.prototype.resolve = function(x) { | ||
@@ -495,6 +478,2 @@ this.become(getHandler(x)); | ||
Delegating.prototype.inspect = function() { | ||
return this.join().inspect(); | ||
}; | ||
Delegating.prototype._report = function(context) { | ||
@@ -524,26 +503,2 @@ this.join()._report(context); | ||
/** | ||
* Handler that follows another handler, injecting a receiver | ||
* @param {object} handler another handler to follow | ||
* @param {object=undefined} receiver | ||
* @constructor | ||
*/ | ||
function Bound(handler, receiver) { | ||
Delegating.call(this, handler); | ||
this.receiver = receiver; | ||
} | ||
inherit(Delegating, Bound); | ||
Bound.prototype.when = function(continuation) { | ||
// Because handlers are allowed to be shared among promises, | ||
// each of which possibly having a different receiver, we have | ||
// to insert our own receiver into the chain if it has been set | ||
// so that callbacks (f, r, u) will be called using our receiver | ||
if(this.receiver !== void 0) { | ||
continuation.receiver = this.receiver; | ||
} | ||
this.join().when(continuation); | ||
}; | ||
/** | ||
* Handler that wraps an untrusted thenable and assimilates it in a future stack | ||
@@ -575,6 +530,2 @@ * @param {function} then | ||
Fulfilled.prototype.inspect = function() { | ||
return { state: 'fulfilled', value: this.value }; | ||
}; | ||
Fulfilled.prototype.fold = function(f, z, c, to) { | ||
@@ -610,6 +561,2 @@ runContinuation3(f, z, this, c, to); | ||
Rejected.prototype.inspect = function() { | ||
return { state: 'rejected', reason: this.value }; | ||
}; | ||
Rejected.prototype.fold = function(f, z, c, to) { | ||
@@ -681,12 +628,2 @@ this._unreport(); | ||
// Snapshot states | ||
/** | ||
* Creates a pending state snapshot | ||
* @returns {{state:'pending'}} | ||
*/ | ||
function toPendingState() { | ||
return { state: 'pending' }; | ||
} | ||
// Task runners | ||
@@ -693,0 +630,0 @@ |
{ | ||
"name": "when", | ||
"version": "3.3.1", | ||
"version": "3.4.0", | ||
"description": "A lightweight Promises/A+ and when() implementation, plus other async goodies.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
29
when.js
@@ -8,3 +8,3 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
* @author John Hann | ||
* @version 3.3.1 | ||
* @version 3.4.0 | ||
*/ | ||
@@ -56,2 +56,3 @@ (function(define) { 'use strict'; | ||
when.map = map; // Array.map() for promises | ||
when.filter = filter; // Array.filter() for promises | ||
when.reduce = reduce; // Array.reduce() for promises | ||
@@ -198,3 +199,3 @@ when.reduceRight = reduceRight; // Array.reduceRight() for promises | ||
* @param {array|Promise} promises array (or promise for an array) of promises | ||
* @returns {Promise} | ||
* @returns {Promise} promise for array of settled state descriptors | ||
*/ | ||
@@ -209,3 +210,4 @@ function settle(promises) { | ||
* @param {Array|Promise} promises array of anything, may contain promises and values | ||
* @param {function} mapFunc map function which may return a promise or value | ||
* @param {function(x:*, index:Number):*} mapFunc map function which may | ||
* return a promise or value | ||
* @returns {Promise} promise that will fulfill with an array of mapped values | ||
@@ -221,2 +223,17 @@ * or reject if any input promise rejects. | ||
/** | ||
* Filter the provided array of promises using the provided predicate. Input may | ||
* contain promises and values | ||
* @param {Array|Promise} promises array of promises and values | ||
* @param {function(x:*, index:Number):boolean} predicate filtering predicate. | ||
* Must return truthy (or promise for truthy) for items to retain. | ||
* @returns {Promise} promise that will fulfill with an array containing all items | ||
* for which predicate returned truthy. | ||
*/ | ||
function filter(promises, predicate) { | ||
return when(promises, function(promises) { | ||
return Promise.filter(promises, predicate); | ||
}); | ||
} | ||
/** | ||
* Traditional reduce function, similar to `Array.prototype.reduce()`, but | ||
@@ -226,6 +243,5 @@ * input may contain promises and/or values, and reduceFunc | ||
* be a promise for the starting value. | ||
* | ||
* @param {Array|Promise} promises array or promise for an array of anything, | ||
* may contain a mix of promises and values. | ||
* @param {function} f reduce function reduce(currentValue, nextValue, index) | ||
* @param {function(accumulated:*, x:*, index:Number):*} f reduce function | ||
* @returns {Promise} that will resolve to the final reduced value | ||
@@ -247,6 +263,5 @@ */ | ||
* be a promise for the starting value. | ||
* | ||
* @param {Array|Promise} promises array or promise for an array of anything, | ||
* may contain a mix of promises and values. | ||
* @param {function} f reduce function reduce(currentValue, nextValue, index) | ||
* @param {function(accumulated:*, x:*, index:Number):*} f reduce function | ||
* @returns {Promise} that will resolve to the final reduced value | ||
@@ -253,0 +268,0 @@ */ |
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
143434
4273