-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added .any() to kew, with tests and docs #51
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,6 @@ logs | |
results | ||
|
||
node_modules | ||
npm-debug.log | ||
npm-debug.log | ||
|
||
.idea/ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -680,6 +680,60 @@ function allInternal(promises) { | |
return promise | ||
} | ||
|
||
/** | ||
* Takes in an array of promises or literals and returns a promise that is | ||
* fulfilled by the first given promise to be fulfilled, or rejected if all of the | ||
* given promises are rejected. | ||
* | ||
* @param {!Array.<!Promise>} promises | ||
* @return {!Promise} | ||
*/ | ||
function any(promises) { | ||
if (arguments.length != 1 || !Array.isArray(promises)) { | ||
promises = Array.prototype.slice.call(arguments, 0) | ||
} | ||
return anyInternal(promises) | ||
} | ||
|
||
/** | ||
* A version of any() that does not accept var_args | ||
* | ||
* @param {!Array.<!Promise>} promises | ||
* @return {!Promise} | ||
*/ | ||
function anyInternal(promises) { | ||
if (!promises.length) return resolve(null) | ||
|
||
var errorOutputs = [] | ||
var output = [] | ||
var finished = false | ||
var promise = new Promise() | ||
var counter = promises.length | ||
|
||
for (var i = 0; i < promises.length; i += 1) { | ||
if(finished) break; | ||
if (!promises[i] || !isPromiseLike(promises[i])) { | ||
promise.resolve(promises[i]); | ||
break; | ||
} else { | ||
promises[i].then(replaceEl.bind(null, output, 0)) | ||
.then(function resolveFirstPromise() { | ||
promise.resolve(output[0]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think you need a guard here if the promise has already been resolved? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, sorry, but I didn't understand that. What do you mean by guard and where? I took cue (read copied) from the allInternal function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you have a race condition. resolveFirstPromise happens asynchronously, On Wed, Jul 15, 2015 at 1:47 PM, Ketan Bhatt [email protected]
|
||
finished = true; | ||
}, function onAnyError(e) { | ||
errorOutputs.push(e); | ||
counter-- | ||
if (!finished && counter === 0) { | ||
finished = true | ||
promise.reject(errorOutputs) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
return promise | ||
} | ||
|
||
/** | ||
* Takes in an array of promises or values and returns a promise that is | ||
* fulfilled with an array of state objects when all have resolved or | ||
|
@@ -828,6 +882,7 @@ function bindPromise(fn, scope, var_args) { | |
|
||
module.exports = { | ||
all: all | ||
, any: any | ||
, bindPromise: bindPromise | ||
, defer: defer | ||
, delay: delay | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for what it's worth, i find it helpful to create a global .gitignore, rather than adding my IDE-specific ignores to every project I contribute to
https://help.github.com/articles/ignoring-files/#create-a-global-gitignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had no clue about this, thank you for sharing. Should I revert the gitignore to its last commit's state?