-
-
Notifications
You must be signed in to change notification settings - Fork 313
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
promise.any() #86
Comments
It's not a very commonly used promise function, and it's non-standard, so I'm not sure I want to add it to the core library. It would not be too difficult to implement as a separate library, something like: function promiseAny(promises) {
if (!Array.isArray(promises)) return Promise.reject(new TypeError('promises must be an array'));
if (promises.length === 0) return Promise.reject(new TypeError('Cannot call any on array of length 0'));
return new Promise(function (resolve, reject) {
var fulfilled = false, rejections = [];
function onFulfill(result) {
fulfilled = true;
resolve(result);
}
function onReject(err) {
if (fulfilled) return;
rejections.push(err);
if (rejections.length === promises.length) {
// TODO: implement AggregateError
reject(new AggregateError(rejections));
}
}
promises.forEach(function (promise) {
promise.then(onFulfill, onReject);
});
});
} I think given how bluebird is currently about the only library to implement this and I've never had need of it myself, I'm not inclined to add it to the promise library. |
@ForbesLindesay Perhaps you are not doing any development that requires logical search patterns, because the pattern where one successful attempt in a set indicates overall success is in fact a very common one. |
@ForbesLindesay How does this differ from |
Consider: var rejected = Promise.reject(new Error('Whatever'));
var fulfilled = new Promise(function (resolve) {
setTimeout(() => resolve('success'), 500);
});
var any = Promise.any([rejected, fulfilled]);
// => fulfilled with 'success' after 500ms
var race = Promise.race([rejected, fulfilled]);
// => immediately rejected with `new Error('Whatever')` |
I've been using promise in my every project, including this one: pg-promise, until I ran into the necessity of using
promise.any()
logic in my test cases, so I had to use Bluebird for testing, while keeping Promise for the production, which is awkward.And now that I find I need
promise.any
in more than one case, it is becoming unfortunate that I have to move to Bluebird completely.Please, add
promise.any
, it is a very important promise function!The text was updated successfully, but these errors were encountered: