Skip to content
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

Open
vitaly-t opened this issue Apr 8, 2015 · 4 comments
Open

promise.any() #86

vitaly-t opened this issue Apr 8, 2015 · 4 comments

Comments

@vitaly-t
Copy link

vitaly-t commented Apr 8, 2015

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!

@ForbesLindesay
Copy link
Member

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.

@vitaly-t
Copy link
Author

vitaly-t commented Apr 9, 2015

@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.

@edef1c
Copy link
Member

edef1c commented Apr 10, 2015

@ForbesLindesay How does this differ from Promise#race, which does appear to be part of ES6?

@ForbesLindesay
Copy link
Member

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')`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants