UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

37 lines (36 loc) 1.07 kB
import { awaitedBlockingMap } from './awaited-map.js'; /** * Performs * [`[].filter()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) * on an array but supports an async callback. * * @category Array * @category Package : @augment-vir/common * @example * * ```ts * import {awaitedFilter} from '@augment-vir/common'; * * const result = await awaitedFilter( * [ * 1, * 2, * 3, * 4, * 5, * ], * async (value) => { * return await Promise.resolve(value > 2); * }, * ); * ``` * * @returns A new array (does not mutate). * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export async function awaitedFilter(arrayInput, filterCallback, options) { const callbackResults = options?.blocking ? await awaitedBlockingMap(arrayInput, filterCallback) : await Promise.all(arrayInput.map(filterCallback)); return arrayInput.filter((originalValue, index) => !!callbackResults[index]); }