Skip to content

Feature request: filter that works with promises #457

@alexander-azizi-martin

Description

@alexander-azizi-martin

I often have to filter a list but may need todo an async request to determine if an item should be kept or not (e.g. checking existence of an item in a database).

Adding a new filter function similar to the already existing map function within radashi would be helpful. Something along the lines of:

export async function filter<T>(
  array: readonly T[],
  asyncFilterFunc: (item: T, index: number) => PromiseLike<boolean>,
): Promise<K[]> {
  if (!array) {
    return []
  }
  const result = []
  let index = 0
  for (const value of array) {
    const keepValue = await asyncFilterFunc(value, index++)
    if (keepValue) {
      result.push(newValue)
    }
  }
  return result
}

If this seems like it can be added to radashi, then I can open a PR with the implementation + tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions