Skip to content

Instantly share code, notes, and snippets.

@qwtel
Last active October 13, 2021 02:11
Show Gist options
  • Select an option

  • Save qwtel/c597421f1dcc6f67a2c5230c13a1a8af to your computer and use it in GitHub Desktop.

Select an option

Save qwtel/c597421f1dcc6f67a2c5230c13a1a8af to your computer and use it in GitHub Desktop.

Revisions

  1. qwtel revised this gist Oct 13, 2021. 1 changed file with 0 additions and 16 deletions.
    16 changes: 0 additions & 16 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,16 +0,0 @@
    Returns the first promise that resolves with a truthy value,
    or `undefined` if all promises resolve with a nullish value.

    Note that this inherits the behavior of `Promise.race`,
    where the returned promise rejects as soon as one input promise rejects.

    Example use case: Getting a cached response from a number of caches,
    without resorting to a lookup in all caches via `caches.match`:

    ```js
    const matching = await raceTruthy([
    caches.open(SHELL_CACHE).then(c => c.match(url)),
    caches.open(ASSETS_CACHE).then(c => c.match(url)),
    caches.open(CONTENT_CACHE).then(c => c.match(url)),
    ]);
    ```
  2. qwtel revised this gist Sep 8, 2019. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions promise-race-truthy.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,12 @@
    const NEVER = new Promise(() => {});

    async function raceTruthy(iterable) {
    const promises = [...iterable].map(_ => Promise.resolve(_));
    let countdown = promises.length;
    const continueWhenNullish = value => value != null ? value : --countdown > 0 ? NEVER : undefined;
    return Promise.race(promises.map(promise => promise.then(continueWhenNullish)));
    const ps = [...iterable].map(_ => Promise.resolve(_));
    let { length } = ps;
    const continueWhenNullish = value => value != null
    ? value
    : --length > 0
    ? NEVER
    : undefined;
    return Promise.race(ps.map(p => p.then(continueWhenNullish)));
    }
  3. qwtel revised this gist Sep 8, 2019. 1 changed file with 4 additions and 9 deletions.
    13 changes: 4 additions & 9 deletions promise-race-truthy.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,8 @@
    const NEVER = new Promise(() => {});

    async function raceTruthy(promises = []) {
    async function raceTruthy(iterable) {
    const promises = [...iterable].map(_ => Promise.resolve(_));
    let countdown = promises.length;
    return Promise.race(
    promises.map(p => p.then(x => x != null
    ? x
    : --countdown > 0
    ? NEVER
    : undefined
    ))
    );
    const continueWhenNullish = value => value != null ? value : --countdown > 0 ? NEVER : undefined;
    return Promise.race(promises.map(promise => promise.then(continueWhenNullish)));
    }
  4. qwtel revised this gist Sep 8, 2019. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions promise-race-truthy.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,13 @@
    const NEVER = new Promise(() => {})
    const NEVER = new Promise(() => {});

    async function raceTruthy(promises = []) {
    let countdown = promises.length;
    const continueIfNullish = x => x != null ? x : --countdown > 0 ? NEVER : undefined;
    return Promise.race(promises.map(p => p.then(continueIfNullish)));
    return Promise.race(
    promises.map(p => p.then(x => x != null
    ? x
    : --countdown > 0
    ? NEVER
    : undefined
    ))
    );
    }
  5. qwtel revised this gist Sep 8, 2019. No changes.
  6. qwtel revised this gist Sep 8, 2019. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    Returns the first promise that resolves with a truthy value,
    or `undefined` if all promises resolve with a nullish value.

    Note that this inherits the behavior of `Promise.race`,
    where the returned promise rejects as soon as one input promise rejects.

    Example use case: Getting a cached response from a number of caches,
    without resorting to a lookup in all caches via `caches.match`:

    ```js
    const matching = await raceTruthy([
    caches.open(SHELL_CACHE).then(c => c.match(url)),
    caches.open(ASSETS_CACHE).then(c => c.match(url)),
    caches.open(CONTENT_CACHE).then(c => c.match(url)),
    ]);
    ```
  7. qwtel created this gist Sep 7, 2019.
    7 changes: 7 additions & 0 deletions promise-race-truthy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    const NEVER = new Promise(() => {})

    async function raceTruthy(promises = []) {
    let countdown = promises.length;
    const continueIfNullish = x => x != null ? x : --countdown > 0 ? NEVER : undefined;
    return Promise.race(promises.map(p => p.then(continueIfNullish)));
    }