Comparing version 3.5.2 to 3.6.0
@@ -130,11 +130,16 @@ !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 logInfo = noop; | ||
var localConsole; | ||
if(typeof console !== 'undefined') { | ||
logError = typeof console.error !== 'undefined' | ||
? function (e) { console.error(e); } | ||
: function (e) { console.log(e); }; | ||
// Alias console to prevent things like uglify's drop_console option from | ||
// removing console.log/error. Unhandled rejections fall into the same | ||
// category as uncaught exceptions, and build tools shouldn't silence them. | ||
localConsole = console; | ||
logError = typeof localConsole.error !== 'undefined' | ||
? function (e) { localConsole.error(e); } | ||
: function (e) { localConsole.log(e); }; | ||
logInfo = typeof console.info !== 'undefined' | ||
? function (e) { console.info(e); } | ||
: function (e) { console.log(e); }; | ||
logInfo = typeof localConsole.info !== 'undefined' | ||
? function (e) { localConsole.info(e); } | ||
: function (e) { localConsole.log(e); }; | ||
} | ||
@@ -156,3 +161,3 @@ | ||
var reported = []; | ||
var running = false; | ||
var running = null; | ||
@@ -176,4 +181,3 @@ function report(r) { | ||
tasks.push(f, x); | ||
if(!running) { | ||
running = true; | ||
if(running === null) { | ||
running = setTimer(flush, 0); | ||
@@ -184,3 +188,3 @@ } | ||
function flush() { | ||
running = false; | ||
running = null; | ||
while(tasks.length > 0) { | ||
@@ -474,2 +478,3 @@ tasks.shift()(tasks.shift()); | ||
Promise.race = race; | ||
Promise._traverse = traverse; | ||
@@ -484,3 +489,19 @@ /** | ||
function all(promises) { | ||
/*jshint maxcomplexity:8*/ | ||
return traverseWith(snd, null, promises); | ||
} | ||
/** | ||
* Array<Promise<X>> -> Promise<Array<f(X)>> | ||
* @private | ||
* @param {function} f function to apply to each promise's value | ||
* @param {Array} promises array of promises | ||
* @returns {Promise} promise for transformed values | ||
*/ | ||
function traverse(f, promises) { | ||
return traverseWith(tryCatch2, f, promises); | ||
} | ||
function traverseWith(tryMap, f, promises) { | ||
var handler = typeof f === 'function' ? mapAt : settleAt; | ||
var resolver = new Pending(); | ||
@@ -490,4 +511,3 @@ var pending = promises.length >>> 0; | ||
var i, h, x, s; | ||
for (i = 0; i < promises.length; ++i) { | ||
for (var i = 0, x; i < promises.length; ++i) { | ||
x = promises[i]; | ||
@@ -500,20 +520,3 @@ | ||
if (maybeThenable(x)) { | ||
h = getHandlerMaybeThenable(x); | ||
s = h.state(); | ||
if (s === 0) { | ||
h.fold(settleAt, i, results, resolver); | ||
} else if (s > 0) { | ||
results[i] = h.value; | ||
--pending; | ||
} else { | ||
resolveAndObserveRemaining(promises, i+1, h, resolver); | ||
break; | ||
} | ||
} else { | ||
results[i] = x; | ||
--pending; | ||
} | ||
traverseAt(promises, handler, i, x, resolver); | ||
} | ||
@@ -527,7 +530,10 @@ | ||
function mapAt(i, x, resolver) { | ||
traverseAt(promises, settleAt, i, tryMap(f, x, i), resolver); | ||
} | ||
function settleAt(i, x, resolver) { | ||
/*jshint validthis:true*/ | ||
this[i] = x; | ||
results[i] = x; | ||
if(--pending === 0) { | ||
resolver.become(new Fulfilled(this)); | ||
resolver.become(new Fulfilled(results)); | ||
} | ||
@@ -537,2 +543,20 @@ } | ||
function traverseAt(promises, handler, i, x, resolver) { | ||
if (maybeThenable(x)) { | ||
var h = getHandlerMaybeThenable(x); | ||
var s = h.state(); | ||
if (s === 0) { | ||
h.fold(handler, i, void 0, resolver); | ||
} else if (s > 0) { | ||
handler(i, h.value, resolver); | ||
} else { | ||
resolveAndObserveRemaining(promises, i+1, h, resolver); | ||
} | ||
} else { | ||
handler(i, x, resolver); | ||
} | ||
} | ||
function resolveAndObserveRemaining(promises, start, handler, resolver) { | ||
@@ -688,5 +712,3 @@ resolver.become(handler); | ||
Handler.prototype.fold = function(f, z, c, to) { | ||
this.visit(to, function(x) { | ||
f.call(c, z, x, this); | ||
}, to.reject, to.notify); | ||
this.when(new Fold(f, z, c, to)); | ||
}; | ||
@@ -910,2 +932,5 @@ | ||
Rejected.prototype._unreport = function() { | ||
if(this.handled) { | ||
return; | ||
} | ||
this.handled = true; | ||
@@ -1028,2 +1053,24 @@ tasks.afterQueue(new UnreportTask(this)); | ||
/** | ||
* Fold a handler value with z | ||
* @constructor | ||
*/ | ||
function Fold(f, z, c, to) { | ||
this.f = f; this.z = z; this.c = c; this.to = to; | ||
this.resolver = failIfRejected; | ||
this.receiver = this; | ||
} | ||
Fold.prototype.fulfilled = function(x) { | ||
this.f.call(this.c, this.z, x, this.to); | ||
}; | ||
Fold.prototype.rejected = function(x) { | ||
this.to.reject(x); | ||
}; | ||
Fold.prototype.progress = function(x) { | ||
this.to.notify(x); | ||
}; | ||
// Other helpers | ||
@@ -1082,2 +1129,10 @@ | ||
function tryCatch2(f, a, b) { | ||
try { | ||
return f(a, b); | ||
} catch(e) { | ||
return reject(e); | ||
} | ||
} | ||
/** | ||
@@ -1123,2 +1178,6 @@ * Return f.call(thisArg, x), or if it throws return a rejected promise for | ||
function snd(x, y) { | ||
return y; | ||
} | ||
function noop() {} | ||
@@ -1125,0 +1184,0 @@ |
@@ -10,2 +10,4 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var slice = Array.prototype.slice; | ||
var Promise = when.Promise; | ||
var reject = Promise.reject; | ||
@@ -57,33 +59,42 @@ /** | ||
function run(generator, thisArg, args) { | ||
var stepper = new Stepper(next, error, generator.apply(thisArg, args)); | ||
return runNext(void 0, generator.apply(thisArg, args)); | ||
} | ||
return stepper.step('next', void 0); | ||
function next(x) { return stepper.step('next', x); } | ||
function error(e) { return stepper.step('throw', e); } | ||
function runNext(x, iterator) { | ||
try { | ||
return handle(iterator.next(x), iterator); | ||
} catch(e) { | ||
return reject(e); | ||
} | ||
} | ||
/** | ||
* Manages the process of stepping the provided iterator | ||
* @constructor | ||
*/ | ||
function Stepper(next, error, iterator) { | ||
this.next = next; | ||
this.error = error; | ||
this.iterator = iterator; | ||
function next(x) { | ||
/*jshint validthis:true*/ | ||
return runNext(x, this); | ||
} | ||
Stepper.prototype.step = function(action, x) { | ||
function error(e) { | ||
/*jshint validthis:true*/ | ||
try { | ||
return this._continue(action, x); | ||
} catch (e) { | ||
return when.reject(e); | ||
return handle(this.throw(e), this); | ||
} catch(e) { | ||
return reject(e); | ||
} | ||
}; | ||
} | ||
Stepper.prototype._continue = function(action, x) { | ||
var result = this.iterator[action](x); | ||
return result.done ? result.value : when(result.value, this.next, this.error); | ||
}; | ||
function handle(result, iterator) { | ||
if(result.done) { | ||
return result.value; | ||
} | ||
var h = Promise._handler(result.value); | ||
if(h.state() > 0) { | ||
return runNext(h.value, iterator); | ||
} | ||
var p = Promise._defer(); | ||
h.chain(p._handler, iterator, next, error); | ||
return p; | ||
} | ||
return { | ||
@@ -90,0 +101,0 @@ lift: lift, |
@@ -11,5 +11,5 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var arrayReduce = Array.prototype.reduce; | ||
var arrayReduceRight = Array.prototype.reduceRight; | ||
var toPromise = Promise.resolve; | ||
var reject = Promise.reject; | ||
var all = Promise.all; | ||
@@ -145,13 +145,3 @@ | ||
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); | ||
} | ||
return Promise._traverse(f, promises); | ||
} | ||
@@ -208,34 +198,68 @@ | ||
* Reduce an array of promises and values | ||
* @param {Array} promises | ||
* @param {Array} a array of promises | ||
* @param {function(accumulated:*, x:*, index:Number):*} f reduce function | ||
* @returns {Promise} promise for reduced value | ||
*/ | ||
function reduce(promises, f) { | ||
var reducer = makeReducer(f); | ||
return arguments.length > 2 | ||
? arrayReduce.call(promises, reducer, arguments[2]) | ||
: arrayReduce.call(promises, reducer); | ||
function reduce(a, f) { | ||
var hasArg = arguments.length > 2; | ||
if((a.length>>>0) === 0) { | ||
return hasArg ? toPromise(arguments[2]) : reject(new TypeError('Cannot reduce empty array with no initial value')); | ||
} | ||
// Skip sparse array holes >:( | ||
// jshint curly:false | ||
for(var i=0; i<a.length && !(i in a); ++i); | ||
return hasArg ? runReduce(i, a, f, arguments[2]) | ||
: runReduce(i + 1, a, f, a[i]); | ||
} | ||
function runReduce(i, a, f, z) { | ||
if (i === a.length) { | ||
return z; | ||
} | ||
var x = a[i]; | ||
if(x === void 0 && !(i in a)) { | ||
return runReduce(i + 1, a, f, z); | ||
} | ||
return toPromise(a[i]).fold(function (z, x) { | ||
return runReduce(i + 1, a, f, f(z, x, i)); | ||
}, z); | ||
} | ||
/** | ||
* Reduce an array of promises and values from the right | ||
* @param {Array} promises | ||
* @param {Array} a array of promises | ||
* @param {function(accumulated:*, x:*, index:Number):*} f reduce function | ||
* @returns {Promise} promise for reduced value | ||
*/ | ||
function reduceRight(promises, f) { | ||
var reducer = makeReducer(f); | ||
return arguments.length > 2 | ||
? arrayReduceRight.call(promises, reducer, arguments[2]) | ||
: arrayReduceRight.call(promises, reducer); | ||
function reduceRight(a, f) { | ||
var hasArg = arguments.length > 2; | ||
if((a.length>>>0) === 0) { | ||
return hasArg ? toPromise(arguments[2]) : reject(new TypeError('Cannot reduce empty array with no initial value')); | ||
} | ||
// Skip sparse array holes >:( | ||
// jshint curly:false | ||
for(var i=a.length-1; i>=0 && !(i in a); i--); | ||
return hasArg ? runReduceRight(i, a, f, arguments[2]) | ||
: runReduceRight(i - 1, a, f, a[i]); | ||
} | ||
function makeReducer(f) { | ||
return function reducer(result, x, i) { | ||
return toPromise(result).then(function(r) { | ||
return toPromise(x).then(function(x) { | ||
return f(r, x, i); | ||
}); | ||
}); | ||
}; | ||
function runReduceRight(i, a, f, z) { | ||
if (i < 0) { | ||
return z; | ||
} | ||
var x = a[i]; | ||
if(x === void 0 && !(i in a)) { | ||
return runReduceRight(i - 1, a, f, z); | ||
} | ||
return toPromise(a[i]).fold(function (z, x) { | ||
return runReduceRight(i - 1, a, f, f(z, x, i)); | ||
}, z); | ||
} | ||
@@ -242,0 +266,0 @@ |
@@ -11,19 +11,10 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
Promise.prototype.inspect = function() { | ||
return inspect(Promise._handler(this)); | ||
}; | ||
function inspect(handler) { | ||
var handler = Promise._handler(this); | ||
var state = handler.state(); | ||
if(state === 0) { | ||
return { state: 'pending' }; | ||
} | ||
return state === 0 ? { state: 'pending' } | ||
: state > 0 ? { state: 'fulfilled', value: handler.value } | ||
: { state: 'rejected', reason: handler.value }; | ||
}; | ||
if(state > 0) { | ||
return { state: 'fulfilled', value: handler.value }; | ||
} | ||
return { state: 'rejected', reason: handler.value }; | ||
} | ||
return Promise; | ||
@@ -30,0 +21,0 @@ }; |
@@ -43,3 +43,3 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var reported = []; | ||
var running = false; | ||
var running = null; | ||
@@ -63,4 +63,3 @@ function report(r) { | ||
tasks.push(f, x); | ||
if(!running) { | ||
running = true; | ||
if(running === null) { | ||
running = setTimer(flush, 0); | ||
@@ -71,3 +70,3 @@ } | ||
function flush() { | ||
running = false; | ||
running = null; | ||
while(tasks.length > 0) { | ||
@@ -74,0 +73,0 @@ tasks.shift()(tasks.shift()); |
@@ -174,2 +174,3 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
Promise.race = race; | ||
Promise._traverse = traverse; | ||
@@ -184,3 +185,19 @@ /** | ||
function all(promises) { | ||
/*jshint maxcomplexity:8*/ | ||
return traverseWith(snd, null, promises); | ||
} | ||
/** | ||
* Array<Promise<X>> -> Promise<Array<f(X)>> | ||
* @private | ||
* @param {function} f function to apply to each promise's value | ||
* @param {Array} promises array of promises | ||
* @returns {Promise} promise for transformed values | ||
*/ | ||
function traverse(f, promises) { | ||
return traverseWith(tryCatch2, f, promises); | ||
} | ||
function traverseWith(tryMap, f, promises) { | ||
var handler = typeof f === 'function' ? mapAt : settleAt; | ||
var resolver = new Pending(); | ||
@@ -190,4 +207,3 @@ var pending = promises.length >>> 0; | ||
var i, h, x, s; | ||
for (i = 0; i < promises.length; ++i) { | ||
for (var i = 0, x; i < promises.length; ++i) { | ||
x = promises[i]; | ||
@@ -200,20 +216,3 @@ | ||
if (maybeThenable(x)) { | ||
h = getHandlerMaybeThenable(x); | ||
s = h.state(); | ||
if (s === 0) { | ||
h.fold(settleAt, i, results, resolver); | ||
} else if (s > 0) { | ||
results[i] = h.value; | ||
--pending; | ||
} else { | ||
resolveAndObserveRemaining(promises, i+1, h, resolver); | ||
break; | ||
} | ||
} else { | ||
results[i] = x; | ||
--pending; | ||
} | ||
traverseAt(promises, handler, i, x, resolver); | ||
} | ||
@@ -227,7 +226,10 @@ | ||
function mapAt(i, x, resolver) { | ||
traverseAt(promises, settleAt, i, tryMap(f, x, i), resolver); | ||
} | ||
function settleAt(i, x, resolver) { | ||
/*jshint validthis:true*/ | ||
this[i] = x; | ||
results[i] = x; | ||
if(--pending === 0) { | ||
resolver.become(new Fulfilled(this)); | ||
resolver.become(new Fulfilled(results)); | ||
} | ||
@@ -237,2 +239,20 @@ } | ||
function traverseAt(promises, handler, i, x, resolver) { | ||
if (maybeThenable(x)) { | ||
var h = getHandlerMaybeThenable(x); | ||
var s = h.state(); | ||
if (s === 0) { | ||
h.fold(handler, i, void 0, resolver); | ||
} else if (s > 0) { | ||
handler(i, h.value, resolver); | ||
} else { | ||
resolveAndObserveRemaining(promises, i+1, h, resolver); | ||
} | ||
} else { | ||
handler(i, x, resolver); | ||
} | ||
} | ||
function resolveAndObserveRemaining(promises, start, handler, resolver) { | ||
@@ -388,5 +408,3 @@ resolver.become(handler); | ||
Handler.prototype.fold = function(f, z, c, to) { | ||
this.visit(to, function(x) { | ||
f.call(c, z, x, this); | ||
}, to.reject, to.notify); | ||
this.when(new Fold(f, z, c, to)); | ||
}; | ||
@@ -610,2 +628,5 @@ | ||
Rejected.prototype._unreport = function() { | ||
if(this.handled) { | ||
return; | ||
} | ||
this.handled = true; | ||
@@ -728,2 +749,24 @@ tasks.afterQueue(new UnreportTask(this)); | ||
/** | ||
* Fold a handler value with z | ||
* @constructor | ||
*/ | ||
function Fold(f, z, c, to) { | ||
this.f = f; this.z = z; this.c = c; this.to = to; | ||
this.resolver = failIfRejected; | ||
this.receiver = this; | ||
} | ||
Fold.prototype.fulfilled = function(x) { | ||
this.f.call(this.c, this.z, x, this.to); | ||
}; | ||
Fold.prototype.rejected = function(x) { | ||
this.to.reject(x); | ||
}; | ||
Fold.prototype.progress = function(x) { | ||
this.to.notify(x); | ||
}; | ||
// Other helpers | ||
@@ -782,2 +825,10 @@ | ||
function tryCatch2(f, a, b) { | ||
try { | ||
return f(a, b); | ||
} catch(e) { | ||
return reject(e); | ||
} | ||
} | ||
/** | ||
@@ -823,2 +874,6 @@ * Return f.call(thisArg, x), or if it throws return a rejected promise for | ||
function snd(x, y) { | ||
return y; | ||
} | ||
function noop() {} | ||
@@ -825,0 +880,0 @@ |
49
node.js
@@ -65,12 +65,15 @@ /** @license MIT License (c) copyright 2013 original author or authors */ | ||
/** | ||
* Apply helper that allows specifying thisArg | ||
* Execute the supplied node-style async function with the provided | ||
* thisArg and arguments, returning a promise for the outcome. | ||
* @private | ||
* @param {function} f | ||
* @param {object} thisArg | ||
* @param {Array} args | ||
* @returns {Promise} | ||
*/ | ||
function run(f, thisArg, args) { | ||
var p = Promise._defer(); | ||
switch(args.length) { | ||
case 2: apply2(p._handler, f, thisArg, args); break; | ||
case 1: apply1(p._handler, f, thisArg, args); break; | ||
default: applyN(p._handler, f, thisArg, args); | ||
} | ||
var l = args.length; | ||
var params = new Array(l); | ||
callAndResolve({ f:f, thisArg:thisArg, args:args, params:params, i:l-1 }, p._handler); | ||
@@ -80,21 +83,25 @@ return p; | ||
function applyN(resolver, f, thisArg, args) { | ||
Promise.all(args)._handler.fold(function(f, args, resolver) { | ||
args.push(createCallback(resolver)); | ||
f.apply(this, args); | ||
}, f, thisArg, resolver); | ||
function callAndResolve(c, h) { | ||
if(c.i < 0) { | ||
return dispatch(c.f, c.thisArg, c.params, createCallback(h)); | ||
} | ||
Promise._handler(c.args[c.i]).fold(callAndResolveNext, c, void 0, h); | ||
} | ||
function apply2(resolver, f, thisArg, args) { | ||
Promise._handler(args[0]).fold(function(x, y, resolver) { | ||
Promise._handler(x).fold(function(x, y, resolver) { | ||
f.call(this, x, y, createCallback(resolver)); | ||
}, y, this, resolver); | ||
}, args[1], thisArg, resolver); | ||
function callAndResolveNext(c, x, h) { | ||
c.params[c.i] = x; | ||
c.i -= 1; | ||
callAndResolve(c, h); | ||
} | ||
function apply1(resolver, f, thisArg, args) { | ||
Promise._handler(args[0]).fold(function(f, x, resolver) { | ||
f.call(this, x, createCallback(resolver)); | ||
}, f, thisArg, resolver); | ||
function dispatch(f, thisArg, args, cb) { | ||
switch(args.length) { | ||
case 2: f.call(thisArg, args[0], args[1], cb); break; | ||
case 1: f.call(thisArg, args[0], cb); break; | ||
case 0: f.call(thisArg, cb); break; | ||
default: | ||
args.push(cb); | ||
f.apply(thisArg, args); | ||
} | ||
} | ||
@@ -101,0 +108,0 @@ |
{ | ||
"name": "when", | ||
"version": "3.5.2", | ||
"version": "3.6.0", | ||
"description": "A lightweight Promises/A+ and when() implementation, plus other async goodies.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -8,3 +8,3 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
* @author John Hann | ||
* @version 3.5.2 | ||
* @version 3.6.0 | ||
*/ | ||
@@ -11,0 +11,0 @@ (function(define) { 'use strict'; |
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
146480
4339