pick
pick iterates over an object’s properties and returns a new object containing only the properties for which the async predicate returns true.
Best for async property filtering Use
pickwhen you need to filter object properties based on an async condition, keeping the result as an object.
Import
Section titled “Import”Root package:
import { pick } from "@vgerbot/async";Module subpath:
import { pick } from "@vgerbot/async/collections";Leaf subpath:
import { pick } from "@vgerbot/async/collections/pick";Quick example
Section titled “Quick example”import { pick } from "@vgerbot/async";
const data = { a: 1, b: 2, c: 3, d: 4 };
const handle = pick( data, async (value, key, token) => { await token.sleep(10); return value % 2 === 0; },);
const result = await handle; // { b: 2, d: 4 }When to use pick
Section titled “When to use pick”- Property filtering: Keep only properties that pass an async condition.
- Object subsetting: Extract a subset of properties based on async validation.
- Async key selection: Select keys based on async computation on values.
- Controlled concurrency: Limit concurrent predicate evaluations.
function pick<T>( obj: Record<string, T> | Promise<Record<string, T>>, predicate: (value: T, key: string, token: CancellableToken) => Promise<boolean>, options?: PickOptions<T>,): CancellableHandle<Record<string, T>>;Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
obj |
Record<string, T> |
— | Input object. Can be a Promise. |
predicate |
(value, key, token) => Promise<boolean> |
— | Async predicate. Return true to keep the property. |
options |
PickOptions<T> |
undefined |
Configuration options. |
PickOptions
Section titled “PickOptions”PickOptions extends CancellableOptions<Record<string, T>> with:
| Option | Type | Default | Description |
|---|---|---|---|
concurrency |
number |
Infinity |
Maximum number of concurrent predicate evaluations. |
Return value
Section titled “Return value”Returns a CancellableHandle<Record<string, T>> that resolves to an object with only the picked properties.
Execution model
Section titled “Execution model”All predicates are evaluated (with optional concurrency limit), then properties are collected into a new object.
Error handling
Section titled “Error handling”If any predicate rejects, pick 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 Record<string, T> — the same value type as the input, with a subset of keys.
const handle = pick( { a: 1, b: 2, c: 3 }, async (value, key) => key !== "a",);
const result = await handle; // { b: 2, c: 3 }