Socket
Socket
Sign inDemoInstall

when

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

when - npm Package Compare versions

Comparing version 3.6.0 to 3.6.1

lib/apply.js

28

callbacks.js

@@ -19,2 +19,6 @@ /** @license MIT License (c) copyright 2013-2014 original author or authors */

var makeApply = require('./lib/apply');
var _apply = makeApply(Promise, dispatch);
var tryCatchResolve = makeApply.tryCatchResolve;
return {

@@ -66,11 +70,5 @@ lift: lift,

*/
function _apply(asyncFunction, thisArg, extraAsyncArgs) {
return Promise.all(extraAsyncArgs).then(function(args) {
var p = Promise._defer();
args.push(alwaysUnary(p._handler.resolve, p._handler),
alwaysUnary(p._handler.reject, p._handler));
asyncFunction.apply(thisArg, args);
return p;
});
function dispatch(f, thisArg, args, h) {
args.push(alwaysUnary(h.resolve, h), alwaysUnary(h.reject, h));
tryCatchResolve(f, thisArg, args, h);
}

@@ -216,7 +214,7 @@

if('callback' in positions) {
if(typeof positions.callback === 'number') {
callbackPos = normalizePosition(args, positions.callback);
}
if('errback' in positions) {
if(typeof positions.errback === 'number') {
errbackPos = normalizePosition(args, positions.errback);

@@ -245,8 +243,4 @@ }

function insertCallback(args, pos, callback, thisArg) {
if(pos != null) {
callback = alwaysUnary(callback, thisArg);
if(pos < 0) {
pos = args.length + pos + 2;
}
args.splice(pos, 0, callback);
if(typeof pos === 'number') {
args.splice(pos, 0, alwaysUnary(callback, thisArg));
}

@@ -253,0 +247,0 @@ }

@@ -505,3 +505,3 @@ !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){

for (var i = 0, x; i < promises.length; ++i) {
for (var i = 0, x; i < promises.length && !resolver.resolved; ++i) {
x = promises[i];

@@ -524,3 +524,5 @@

function mapAt(i, x, resolver) {
traverseAt(promises, settleAt, i, tryMap(f, x, i), resolver);
if(!resolver.resolved) {
traverseAt(promises, settleAt, i, tryMap(f, x, i), resolver);
}
}

@@ -546,5 +548,5 @@

} else {
resolveAndObserveRemaining(promises, i+1, h, resolver);
resolver.become(h);
visitRemaining(promises, i+1, h);
}
} else {

@@ -555,15 +557,20 @@ handler(i, x, resolver);

function resolveAndObserveRemaining(promises, start, handler, resolver) {
resolver.become(handler);
Promise._visitRemaining = visitRemaining;
function visitRemaining(promises, start, handler) {
for(var i=start; i<promises.length; ++i) {
markAsHandled(getHandler(promises[i]), handler);
}
}
var i, h, x;
for(i=start; i<promises.length; ++i) {
x = promises[i];
if(maybeThenable(x)) {
h = getHandlerMaybeThenable(x);
if(h !== handler) {
h.visit(h, void 0, h._unreport);
}
}
function markAsHandled(h, handler) {
if(h === handler) {
return;
}
var s = h.state();
if(s === 0) {
h.visit(h, void 0, h._unreport);
} else if(s < 0) {
h._unreport();
}
}

@@ -608,7 +615,8 @@

if(h.state() !== 0) {
resolveAndObserveRemaining(promises, i+1, h, resolver);
resolver.become(h);
visitRemaining(promises, i+1, h);
break;
} else {
h.visit(resolver, resolver.resolve, resolver.reject);
}
h.visit(resolver, resolver.resolve, resolver.reject);
}

@@ -615,0 +623,0 @@ return new Promise(Handler, resolver);

@@ -17,2 +17,3 @@ /** @license MIT License (c) copyright 2013-2014 original author or authors */

var _liftAll = require('./lib/liftAll');
var _apply = require('./lib/apply')(when.Promise);
var slice = Array.prototype.slice;

@@ -61,13 +62,2 @@

/**
* Apply helper that allows specifying thisArg
* @private
*/
function _apply(f, thisArg, args) {
// args MUST be an Array
return args.length === 0
? attempt.call(thisArg, f)
: attempt.apply(thisArg, [f].concat(args));
}
/**
* Lift all the functions/methods on src

@@ -74,0 +64,0 @@ * @param {object|function} src source whose functions will be lifted

@@ -6,12 +6,16 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */

(function(define) { 'use strict';
define(function() {
define(function(require) {
var state = require('../state');
var applier = require('../apply');
return function array(Promise) {
var arrayReduce = Array.prototype.reduce;
var applyFold = applier(Promise, dispatch);
var toPromise = Promise.resolve;
var reject = Promise.reject;
var all = Promise.all;
var ar = Array.prototype.reduce;
var arr = Array.prototype.reduceRight;
// Additional array combinators

@@ -36,3 +40,3 @@

return this.then(all).then(function(array) {
return onFulfilled.apply(void 0, array);
return onFulfilled.apply(this, array);
});

@@ -52,17 +56,49 @@ };

function any(promises) {
return new Promise(function(resolve, reject) {
var errors = [];
var pending = initRace(promises, resolve, handleReject);
var p = Promise._defer();
var resolver = p._handler;
var l = promises.length>>>0;
if(pending === 0) {
reject(new RangeError('any() input must not be empty'));
var pending = l;
var errors = [];
for (var h, x, i = 0; i < l; ++i) {
x = promises[i];
if(x === void 0 && !(i in promises)) {
--pending;
continue;
}
function handleReject(e) {
errors.push(e);
if(--pending === 0) {
reject(errors);
}
h = Promise._handler(x);
if(h.state() > 0) {
resolver.become(h);
Promise._visitRemaining(promises, i, h);
break;
} else {
h.visit(resolver, handleFulfill, handleReject);
}
});
}
if(pending === 0) {
resolver.reject(new RangeError('any(): array must not be empty'));
}
return p;
function handleFulfill(x) {
/*jshint validthis:true*/
errors = null;
this.resolve(x); // this === resolver
}
function handleReject(e) {
/*jshint validthis:true*/
if(this.resolved) { // this === resolver
return;
}
errors.push(e);
if(--pending === 0) {
this.reject(errors);
}
}
}

@@ -83,56 +119,72 @@

function some(promises, n) {
return new Promise(function(resolve, reject, notify) {
var results = [];
var errors = [];
var nReject;
var nFulfill = initRace(promises, handleResolve, handleReject, notify);
/*jshint maxcomplexity:7*/
var p = Promise._defer();
var resolver = p._handler;
n = Math.max(n, 0);
nReject = (nFulfill - n + 1);
nFulfill = Math.min(n, nFulfill);
var results = [];
var errors = [];
if(n > nFulfill) {
reject(new RangeError('some() input must contain at least '
+ n + ' element(s), but had ' + nFulfill));
} else if(nFulfill === 0) {
resolve(results);
var l = promises.length>>>0;
var nFulfill = 0;
var nReject;
var x, i; // reused in both for() loops
// First pass: count actual array items
for(i=0; i<l; ++i) {
x = promises[i];
if(x === void 0 && !(i in promises)) {
continue;
}
++nFulfill;
}
function handleResolve(x) {
if(nFulfill > 0) {
--nFulfill;
results.push(x);
// Compute actual goals
n = Math.max(n, 0);
nReject = (nFulfill - n + 1);
nFulfill = Math.min(n, nFulfill);
if(nFulfill === 0) {
resolve(results);
}
}
if(n > nFulfill) {
resolver.reject(new RangeError('some(): array must contain at least '
+ n + ' item(s), but had ' + nFulfill));
} else if(nFulfill === 0) {
resolver.resolve(results);
}
// Second pass: observe each array item, make progress toward goals
for(i=0; i<l; ++i) {
x = promises[i];
if(x === void 0 && !(i in promises)) {
continue;
}
function handleReject(e) {
if(nReject > 0) {
--nReject;
errors.push(e);
Promise._handler(x).visit(resolver, fulfill, reject, resolver.notify);
}
if(nReject === 0) {
reject(errors);
}
}
return p;
function fulfill(x) {
/*jshint validthis:true*/
if(this.resolved) { // this === resolver
return;
}
});
}
/**
* 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) {
return arrayReduce.call(promises, function(pending, p) {
toPromise(p).then(resolve, reject, notify);
return pending + 1;
}, 0);
results.push(x);
if(--nFulfill === 0) {
errors = null;
this.resolve(results);
}
}
function reject(e) {
/*jshint validthis:true*/
if(this.resolved) { // this === resolver
return;
}
errors.push(e);
if(--nReject === 0) {
results = null;
this.reject(errors);
}
}
}

@@ -161,21 +213,20 @@

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 Promise._traverse(predicate, promises).then(function(keep) {
return filterSync(promises, keep);
});
}
function filterSync(promises, keep) {
// Safe because we know all promises have fulfilled if we've made it this far
var l = keep.length;
var filtered = new Array(l);
for(var i=0, j=0; i<l; ++i) {
if(keep[i]) {
filtered[j++] = Promise._handler(promises[i]).value;
}
}
filtered.length = j;
return filtered;
}
/**

@@ -185,103 +236,57 @@ * Return a promise that will always fulfill with an array containing

* will never reject.
* @param {array} promises
* @param {Array} promises
* @returns {Promise} promise for array of settled state descriptors
*/
function settle(promises) {
return all(mapArray(function(p) {
p = toPromise(p);
return p.then(inspect, inspect);
return all(promises.map(settleOne));
}
function inspect() {
return p.inspect();
}
}, promises));
function settleOne(p) {
var h = Promise._handler(p);
return h.state() === 0 ? toPromise(p).then(state.fulfilled, state.rejected)
: state.inspect(h);
}
/**
* Reduce an array of promises and values
* @param {Array} a array of promises
* Traditional reduce function, similar to `Array.prototype.reduce()`, but
* input may contain promises and/or values, and reduceFunc
* may return either a value or a promise, *and* initialValue may
* 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(accumulated:*, x:*, index:Number):*} f reduce function
* @returns {Promise} promise for reduced value
* @returns {Promise} that will resolve to the final reduced value
*/
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 reduce(promises, f /*, initialValue */) {
return arguments.length > 2 ? ar.call(promises, liftCombine(f), arguments[2])
: ar.call(promises, liftCombine(f));
}
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} a array of promises
* Traditional reduce function, similar to `Array.prototype.reduceRight()`, but
* input may contain promises and/or values, and reduceFunc
* may return either a value or a promise, *and* initialValue may
* 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(accumulated:*, x:*, index:Number):*} f reduce function
* @returns {Promise} promise for reduced value
* @returns {Promise} that will resolve to the final reduced value
*/
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 reduceRight(promises, f /*, initialValue */) {
return arguments.length > 2 ? arr.call(promises, liftCombine(f), arguments[2])
: arr.call(promises, liftCombine(f));
}
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);
function liftCombine(f) {
return function(z, x, i) {
return applyFold(f, void 0, [z,x,i]);
};
}
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;
function dispatch(f, _, args, resolver) {
resolver.resolve(f(args[0], args[1], args[2]));
}
};
});
}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }));
}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));

@@ -38,9 +38,9 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */

return origCatch.call(this, onRejected);
} else {
if(typeof onRejected !== 'function') {
return this.ensure(rejectInvalidPredicate);
}
}
return origCatch.call(this, createCatchFilter(arguments[1], onRejected));
if(typeof onRejected !== 'function') {
return this.ensure(rejectInvalidPredicate);
}
return origCatch.call(this, createCatchFilter(arguments[1], onRejected));
};

@@ -47,0 +47,0 @@

@@ -6,13 +6,10 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */

(function(define) { 'use strict';
define(function() {
define(function(require) {
var inspect = require('../state').inspect;
return function inspection(Promise) {
Promise.prototype.inspect = function() {
var handler = Promise._handler(this);
var state = handler.state();
return state === 0 ? { state: 'pending' }
: state > 0 ? { state: 'fulfilled', value: handler.value }
: { state: 'rejected', reason: handler.value };
return inspect(Promise._handler(this));
};

@@ -24,2 +21,2 @@

});
}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }));
}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));

@@ -205,3 +205,3 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */

for (var i = 0, x; i < promises.length; ++i) {
for (var i = 0, x; i < promises.length && !resolver.resolved; ++i) {
x = promises[i];

@@ -224,3 +224,5 @@

function mapAt(i, x, resolver) {
traverseAt(promises, settleAt, i, tryMap(f, x, i), resolver);
if(!resolver.resolved) {
traverseAt(promises, settleAt, i, tryMap(f, x, i), resolver);
}
}

@@ -246,5 +248,5 @@

} else {
resolveAndObserveRemaining(promises, i+1, h, resolver);
resolver.become(h);
visitRemaining(promises, i+1, h);
}
} else {

@@ -255,15 +257,20 @@ handler(i, x, resolver);

function resolveAndObserveRemaining(promises, start, handler, resolver) {
resolver.become(handler);
Promise._visitRemaining = visitRemaining;
function visitRemaining(promises, start, handler) {
for(var i=start; i<promises.length; ++i) {
markAsHandled(getHandler(promises[i]), handler);
}
}
var i, h, x;
for(i=start; i<promises.length; ++i) {
x = promises[i];
if(maybeThenable(x)) {
h = getHandlerMaybeThenable(x);
if(h !== handler) {
h.visit(h, void 0, h._unreport);
}
}
function markAsHandled(h, handler) {
if(h === handler) {
return;
}
var s = h.state();
if(s === 0) {
h.visit(h, void 0, h._unreport);
} else if(s < 0) {
h._unreport();
}
}

@@ -308,7 +315,8 @@

if(h.state() !== 0) {
resolveAndObserveRemaining(promises, i+1, h, resolver);
resolver.become(h);
visitRemaining(promises, i+1, h);
break;
} else {
h.visit(resolver, resolver.resolve, resolver.reject);
}
h.visit(resolver, resolver.resolve, resolver.reject);
}

@@ -315,0 +323,0 @@ return new Promise(Handler, resolver);

@@ -15,3 +15,2 @@ /** @license MIT License (c) copyright 2013 original author or authors */

var when = require('./when');
var Promise = when.Promise;
var _liftAll = require('./lib/liftAll');

@@ -21,2 +20,4 @@ var setTimer = require('./lib/env').setTimer;

var _apply = require('./lib/apply')(when.Promise, dispatch);
return {

@@ -63,48 +64,21 @@ lift: lift,

function apply(f, args) {
return run(f, this, args || []);
return _apply(f, this, args || []);
}
/**
* 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();
var l = args.length;
var params = new Array(l);
callAndResolve({ f:f, thisArg:thisArg, args:args, params:params, i:l-1 }, p._handler);
return p;
}
function callAndResolve(c, h) {
if(c.i < 0) {
return dispatch(c.f, c.thisArg, c.params, createCallback(h));
function dispatch(f, thisArg, args, h) {
var cb = createCallback(h);
try {
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);
}
} catch(e) {
h.reject(e);
}
Promise._handler(c.args[c.i]).fold(callAndResolveNext, c, void 0, h);
}
function callAndResolveNext(c, x, h) {
c.params[c.i] = x;
c.i -= 1;
callAndResolve(c, h);
}
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);
}
}
/**

@@ -136,3 +110,3 @@ * Has the same behavior that {@link apply} has, with the difference that the

function call(f /*, args... */) {
return run(f, this, slice.call(arguments, 1));
return _apply(f, this, slice.call(arguments, 1));
}

@@ -184,3 +158,3 @@

}
return run(f, this, args);
return _apply(f, this, args);
};

@@ -187,0 +161,0 @@ }

{
"name": "when",
"version": "3.6.0",
"version": "3.6.1",
"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.6.0
* @version 3.6.1
*/

@@ -31,3 +31,3 @@ (function(define) { 'use strict';

var slice = Array.prototype.slice;
var apply = require('./lib/apply')(Promise);

@@ -58,4 +58,4 @@ // Public API

when.filter = filter; // Array.filter() for promises
when.reduce = reduce; // Array.reduce() for promises
when.reduceRight = reduceRight; // Array.reduceRight() for promises
when.reduce = lift(Promise.reduce); // Array.reduce() for promises
when.reduceRight = lift(Promise.reduceRight); // Array.reduceRight() for promises

@@ -114,3 +114,6 @@ when.isPromiseLike = isPromiseLike; // Is something promise-like, aka thenable

return function() {
return _apply(f, this, slice.call(arguments));
for(var i=0, l=arguments.length, a=new Array(l); i<l; ++i) {
a[i] = arguments[i];
}
return apply(f, this, a);
};

@@ -127,16 +130,9 @@ }

/*jshint validthis:true */
return _apply(f, this, slice.call(arguments, 1));
for(var i=0, l=arguments.length-1, a=new Array(l); i<l; ++i) {
a[i] = arguments[i+1];
}
return apply(f, this, a);
}
/**
* try/lift helper that allows specifying thisArg
* @private
*/
function _apply(f, thisArg, args) {
return Promise.all(args).then(function(args) {
return f.apply(thisArg, args);
});
}
/**
* Creates a {promise, resolver} pair, either or both of which

@@ -238,42 +234,4 @@ * may be given out safely to consumers.

/**
* Traditional reduce function, similar to `Array.prototype.reduce()`, but
* input may contain promises and/or values, and reduceFunc
* may return either a value or a promise, *and* initialValue may
* 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(accumulated:*, x:*, index:Number):*} f reduce function
* @returns {Promise} that will resolve to the final reduced value
*/
function reduce(promises, f /*, initialValue */) {
/*jshint unused:false*/
var args = slice.call(arguments, 1);
return when(promises, function(array) {
args.unshift(array);
return Promise.reduce.apply(Promise, args);
});
}
/**
* Traditional reduce function, similar to `Array.prototype.reduceRight()`, but
* input may contain promises and/or values, and reduceFunc
* may return either a value or a promise, *and* initialValue may
* 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(accumulated:*, x:*, index:Number):*} f reduce function
* @returns {Promise} that will resolve to the final reduced value
*/
function reduceRight(promises, f /*, initialValue */) {
/*jshint unused:false*/
var args = slice.call(arguments, 1);
return when(promises, function(array) {
args.unshift(array);
return Promise.reduceRight.apply(Promise, args);
});
}
return when;
});
})(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc