Skip to content

Instantly share code, notes, and snippets.

@xwlee
Created June 7, 2023 04:48
Show Gist options
  • Save xwlee/1bb9c7a37f1527f6b238b45e1c768893 to your computer and use it in GitHub Desktop.
Save xwlee/1bb9c7a37f1527f6b238b45e1c768893 to your computer and use it in GitHub Desktop.

Revisions

  1. xwlee created this gist Jun 7, 2023.
    12 changes: 12 additions & 0 deletions filterAsync.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    const filterAsync = <T>(arr: T[], fn: (x: T) => Promise<boolean>) =>
    mapAsync(arr, fn).then((arr2) => arr.filter((_, i) => Boolean(arr2[i])));

    (async () => {
    console.log("START FILTER");
    const filtered = await filterAsync([1, 2, 3, 4], async (n) => {
    const x = await fakeFilter(n);
    return x;
    });
    useResult(filtered);
    console.log("END FILTER");
    })();