Comparing version 0.3.0 to 0.3.1
98
kew.js
@@ -0,7 +1,12 @@ | ||
/** @typedef {function(?, ?)} */ | ||
var OnSuccessCallbackType; | ||
/** @typedef {function(!Error, ?)} */ | ||
var OnFailCallbackType; | ||
/** | ||
* An object representing a "promise" for a future value | ||
* | ||
* @param {?function(*, *)=} onSuccess a function to handle successful | ||
* @param {?OnSuccessCallbackType=} onSuccess a function to handle successful | ||
* resolution of this promise | ||
* @param {function(!Error, *)=} onFail a function to handle failed | ||
* @param {OnFailCallbackType=} onFail a function to handle failed | ||
* resolution of this promise | ||
@@ -139,4 +144,4 @@ * @constructor | ||
* | ||
* @param {?function(*, *)} onSuccess | ||
* @param {function(!Error, *)=} onFail | ||
* @param {?OnSuccessCallbackType} onSuccess | ||
* @param {OnFailCallbackType=} onFail | ||
* @return {!Promise} returns a new promise with the output of the onSuccess or | ||
@@ -158,3 +163,3 @@ * onFail handler | ||
* | ||
* @param {function(!Error, *=)} onFail | ||
* @param {OnFailCallbackType} onFail | ||
* @return {!Promise} returns a new promise with the output of the onFail handler | ||
@@ -280,3 +285,3 @@ */ | ||
* | ||
* @return {function(Error, *)} node-style callback | ||
* @return {function(?Error, *)} node-style callback | ||
*/ | ||
@@ -352,7 +357,41 @@ Promise.prototype.makeNodeResolver = function () { | ||
/** | ||
* Replace an element in an array as it is resolved with its value. | ||
* Used by .allSettled(). | ||
* | ||
* @param {!Array} arr | ||
* @param {number} idx | ||
* @param {*} value The value from a resolved promise. | ||
* @return {*} the data that's being passed in | ||
*/ | ||
function replaceElFulfilled(arr, idx, value) { | ||
arr[idx] = { | ||
state: 'fulfilled', | ||
value: value | ||
} | ||
return value | ||
} | ||
/** | ||
* Replace an element in an array as it is rejected with the reason. | ||
* Used by .allSettled(). | ||
* | ||
* @param {!Array} arr | ||
* @param {number} idx | ||
* @param {*} reason The reason why the original promise is rejected | ||
* @return {*} the data that's being passed in | ||
*/ | ||
function replaceElRejected(arr, idx, reason) { | ||
arr[idx] = { | ||
state: 'rejected', | ||
reason: reason | ||
} | ||
return reason | ||
} | ||
/** | ||
* Takes in an array of promises or literals and returns a promise which returns | ||
* an array of values when all have resolved. If any fail, the promise fails. | ||
* | ||
* @param {!Array} promises | ||
* @return {!Promise.<!Array>} | ||
* @param {!Array.<!Promise>} promises | ||
* @return {!Promise} | ||
*/ | ||
@@ -400,2 +439,38 @@ function all(promises) { | ||
/** | ||
* Takes in an array of promises or literals and returns a promise which returns | ||
* an array of values when all have resolved or rejected. | ||
* | ||
* @param {!Array.<!Promise>} promises | ||
* @return {!Array.<Object>} The state of the promises. If a promise is resolved, | ||
* its corresponding state object is {state: 'fulfilled', value: Object}; | ||
* whereas if a promise is rejected, its corresponding state object is | ||
* {state: 'rejected', reason: Object} | ||
*/ | ||
function allSettled(promises) { | ||
if (!Array.isArray(promises)) { | ||
throw Error('The input to "allSettled()" should be an array of Promise') | ||
} | ||
if (!promises.length) return resolve([]) | ||
var outputs = [] | ||
var promise = new Promise() | ||
var counter = promises.length | ||
for (var i = 0; i < promises.length; i += 1) { | ||
if (!promises[i] || !isPromiseLike(promises[i])) { | ||
replaceElFulfilled(outputs, i, promises[i]) | ||
if ((--counter) === 0) promise.resolve(outputs) | ||
} else { | ||
promises[i] | ||
.then(replaceElFulfilled.bind(null, outputs, i), replaceElRejected.bind(null, outputs, i)) | ||
.then(function () { | ||
if ((--counter) === 0) promise.resolve(outputs) | ||
}) | ||
} | ||
} | ||
return promise | ||
} | ||
/** | ||
* Create a new Promise which looks like a deferred | ||
@@ -428,3 +503,3 @@ * | ||
* | ||
* @param {function()} fn | ||
* @param {function(...)} fn | ||
* @param {...} var_args a variable number of arguments | ||
@@ -447,3 +522,3 @@ * @return {!Promise} | ||
* | ||
* @param {function()} fn | ||
* @param {function(...)} fn | ||
* @param {...} var_args a variable number of arguments | ||
@@ -464,3 +539,3 @@ * @return {!Promise} | ||
* | ||
* @param {function()} fn | ||
* @param {function(...)} fn | ||
* @param {Object} scope | ||
@@ -490,2 +565,3 @@ * @param {...} var_args a variable number of arguments | ||
, reject: reject | ||
, allSettled: allSettled | ||
} |
{ | ||
"name": "kew" | ||
, "description": "a lightweight promise library for node" | ||
, "version": "0.3.0" | ||
, "version": "0.3.1" | ||
, "homepage": "https://github.com/Obvious/kew" | ||
, "authors": [ | ||
"Jeremy Stanley <github@azulus.com> (https://github.com/azulus)" | ||
"Jeremy Stanley <github@azulus.com> (https://github.com/azulus)", | ||
"Nick Santos <nick@medium.com>", | ||
"Xiao Ma <x@medium.com>" | ||
] | ||
@@ -9,0 +11,0 @@ , "contributors": [ |
@@ -363,1 +363,17 @@ var Q = require('../kew') | ||
} | ||
exports.testAllSettled = function(test) { | ||
var promise1 = Q.resolve('woot') | ||
var promise2 = Q.reject(new Error('oops')) | ||
Q.allSettled([promise1, promise2, 'just a string']) | ||
.then(function (data) { | ||
test.equals('fulfilled', data[0].state) | ||
test.equals('woot', data[0].value) | ||
test.equals('rejected', data[1].state) | ||
test.equals('oops', data[1].reason.message) | ||
test.equals('fulfilled', data[2].state) | ||
test.equals('just a string', data[2].value) | ||
test.done() | ||
}) | ||
} |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
59432
12
1346
1