Last active
October 13, 2021 02:11
-
-
Save qwtel/c597421f1dcc6f67a2c5230c13a1a8af to your computer and use it in GitHub Desktop.
Revisions
-
qwtel revised this gist
Oct 13, 2021 . 1 changed file with 0 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +0,0 @@ -
qwtel revised this gist
Sep 8, 2019 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,12 @@ const NEVER = new Promise(() => {}); async function raceTruthy(iterable) { 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))); } -
qwtel revised this gist
Sep 8, 2019 . 1 changed file with 4 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,8 @@ 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))); } -
qwtel revised this gist
Sep 8, 2019 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,13 @@ const NEVER = new Promise(() => {}); async function raceTruthy(promises = []) { let countdown = promises.length; return Promise.race( promises.map(p => p.then(x => x != null ? x : --countdown > 0 ? NEVER : undefined )) ); } -
qwtel revised this gist
Sep 8, 2019 . No changes.There are no files selected for viewing
-
qwtel revised this gist
Sep 8, 2019 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)), ]); ``` -
qwtel created this gist
Sep 7, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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))); }