Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kew

Package Overview
Dependencies
Maintainers
5
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kew - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

test/closure_test.js

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
}

6

package.json
{
"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()
})
}
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