partition
partition evaluates an async predicate for each item and returns a tuple [truthy, falsy] where truthy contains items that passed and falsy contains items that failed.
Best for bifurcation Use
partitionwhen you need to split a collection into two groups based on an async condition.
Import
Section titled “Import”Root package:
import { partition } from "@vgerbot/async";Module subpath:
import { partition } from "@vgerbot/async/collections";Leaf subpath:
import { partition } from "@vgerbot/async/collections/partition";Quick example
Section titled “Quick example”import { partition } from "@vgerbot/async";
const handle = partition( [1, 2, 3, 4, 5], async (item) => item % 2 === 0,);
const [evens, odds] = await handle; // [[2, 4], [1, 3, 5]]When to use partition
Section titled “When to use partition”- Bifurcation: Split items into pass/fail groups.
- Validation separation: Separate valid and invalid items.
- Work distribution: Route items to different processing pipelines.
- Filtering with retention: Keep both matching and non-matching items.
// Array inputfunction partition<I>( data: I[] | Promise<I[]>, predicate: (item: I, token: CancellableToken) => Promise<boolean>, options?: PartitionOptions<I>,): CancellableHandle<[I[], I[]]>;
// Object inputfunction partition<I>( data: CollectionInput<I> | Promise<CollectionInput<I>>, predicate: (item: I, key: number | string, token: CancellableToken) => Promise<boolean>, options?: PartitionOptions<I>,): CancellableHandle<[I[], I[]]>;Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
data |
I[] or Record<string, I> |
— | Input collection. Can be a Promise. |
predicate |
(item, token) => Promise<boolean> or (item, key, token) => Promise<boolean> |
— | Async predicate. Return true for the first group, false for the second. |
options |
PartitionOptions<I> |
undefined |
Configuration options. |
PartitionOptions
Section titled “PartitionOptions”PartitionOptions extends CancellableOptions<[I[], I[]]> with:
| Option | Type | Default | Description |
|---|---|---|---|
concurrency |
number |
Infinity |
Maximum number of concurrent predicate evaluations. |
Return value
Section titled “Return value”Returns a CancellableHandle<[I[], I[]]> — a tuple of [truthyItems, falsyItems].
Execution model
Section titled “Execution model”All predicates are evaluated (with optional concurrency limit), then items are split into two arrays based on the results. Order within each group follows the original input order.
Error handling
Section titled “Error handling”If any predicate rejects, partition rejects with the first error.
Cancellation
Section titled “Cancellation”All evaluations share a single CancellableToken. Calling cancel() signals cancellation.
TypeScript tips
Section titled “TypeScript tips”The return type is a tuple [I[], I[]]. Use destructuring for clean access:
const [passed, failed] = await partition( items, async (item) => item.isValid,);