/**
 * Will resolve on the first fulfilled promise and disregard the remaining ones. Similar to `Promise.race` but won't
 * care about rejected promises.
 * @param promises
 */
export var waitForFirstFulfilledPromise = function waitForFirstFulfilledPromise(promises) {
  var rejectReasons = [];
  return new Promise(function (resolve, reject) {
    promises.forEach(function (promise) {
      return promise.then(function (value) {
        if (typeof value === 'undefined' || value === null) {
          throw new Error("Result was not found but the method didn't reject/throw. Please ensure that it doesn't return null or undefined.");
        }
        resolve(value);
      }).catch(function (reason) {
        rejectReasons.push(reason);
        if (rejectReasons.length === promises.length) {
          reject(reason);
        }
      });
    });
  });
};