promise-any-polyfill

A polyfill for the Promise.any function.

Usage no npm install needed!

<script type="module">
  import promiseAnyPolyfill from 'https://cdn.skypack.dev/promise-any-polyfill';
</script>

README

Promise.any Polyfill

A polyfill for the Promise.any function.

Find promise-any-polyfill on NPM.

Table of Contents

Installation

Install from NPM with

$ npm install --save promise-any-polyfill

Basic Usage

This package implements the upcoming Promise.any(...) functionality, and it can be used as follows:

Promise.any([
    timedResolvingPromise(50, "what's up?"),
    timedResolvingPromise(25, "I'm quick!"),
    timedResolvingPromise(75, "I'm late!")
]).then((result: string): void => console.log(result)); //=> "I'm quick!"

As you can see from the above example, Promise.any will resolve to the value of the first resolving Promise that was passed to the function`.

If all promises reject, Promise.any will reject with an array of each of the rejection values from the Promises passed to the function.

Promise.any([
    timedRejectingPromise(50, "...somewhere down the middle..."),
    timedRejectingPromise(25, "I failed first!"),
    timedRejectingPromise(75, "...at least I failed last!")
]).catch((error: string): void => console.error(error)); //=> ["I failed first!", "...somewhere down the middle...", "...at least I failed last!"]

Promise.any will ignore any rejecting Promises so long as at least one of the Promises passed to the function resolves.

Promise.any([
    timedRejectingPromise(25, "I'm first, but I'm failing!"),
    timedResolvingPromise(50, "I'm the only one resolving!"),
    timedRejectingPromise(75, "I'm last AND failing!")
]).then((result: string): void => console.log(result)); //=> "I'm the only one resolving!"

Documentation

See the wiki for full documentation.

License

promise-any-polyfill is made available under the GNU General Public License v3.

Copyright (C) 2021 Trevor Sears