How to use the promise.any function in promise

To help you get started, we’ve selected a few promise examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github shalvah / strive / src / index.ts View on Github external
if (race) {
        const trials = strategies.map(strategy => {
            log(`Trying strategy ${strategy.name}`);
            return strategy().then((result: any) => {
                if (check(result)) {
                    return {strategy, result};
                }
                return Promise.reject({strategy, result});
            }).catch((error: any) => {
                return Promise.reject({strategy, error});
            });
        });

        let firstSuccess;
        try {
            firstSuccess = await any(trials);
        } catch (e) {
            const lastError = e.errors.pop();
            if (ignoreErrors) {
                return returnFailure(lastError.strategy.name, lastError.result, defaultValue);
            } else {
                if (lastError.error) throw lastError.error;
                else return returnFailure(lastError.strategy.name, lastError.result, defaultValue);
            }
        }

        const {strategy, result} = firstSuccess;
        return returnSuccess(strategy.name, result);

    } else {
        let strategy, result;
github jsonwebtoken / jsonwebtoken.github.io / src / editor / jwt.js View on Github external
function getJoseKey(header, key, base64Secret) {
  if(header.alg.indexOf('HS') === 0) {
    return jose.JWK.asKey({
      kty: 'oct',
      use: 'sig',
      alg: header.alg,
      k: paddedKey(key, header.alg, base64Secret)
    });
  } else {
    if(header.alg.indexOf('RS') === 0) {
      key = plainRsaKeyToX509Key(key);
    }

    return any(['pem', 'json'].map(form => {
      try {
        return jose.JWK.asKey(key, form);
      } catch(e) {
        return Promise.reject(e);
      }
    }));
  }
}
github shalvah / strive / src / index.js View on Github external
const striveWithValues = async ({ values = [[]], action, check, ignoreErrors = true, race = false, defaultValue, }) => {
    if (race) {
        const trials = values.map((value, index) => {
            log(`Trying value at ${index}: ${JSON.stringify(value)}`);
            return action(...value).then((result) => {
                if (check(result)) {
                    return { valueIndex: index, result };
                }
                return Promise.reject({ valueIndex: index, result });
            }).catch((error) => {
                return Promise.reject({ valueIndex: index, error });
            });
        });
        let firstSuccess;
        try {
            firstSuccess = await any(trials);
        }
        catch (e) {
            const lastError = e.errors.pop();
            if (ignoreErrors) {
                return utils_1.returnFailure(lastError.valueIndex, lastError.result, defaultValue);
            }
            else {
                if (lastError.error)
                    throw lastError.error;
                else
                    return utils_1.returnFailure(lastError.valueIndex, lastError.result, defaultValue);
            }
        }
        const { valueIndex, result } = firstSuccess;
        return utils_1.returnSuccess(valueIndex, result);
    }
github eclipse / codewind / src / pfe / portal / modules / metricsService / index.js View on Github external
const findAsync = (array, asyncCallback) => promiseAny(
  array.map(element => asyncCallback(element))
)