some
some checks whether any item in a collection passes an async predicate. It short-circuits scheduling when the first true is found.
Best for existential checks Use
somewhen you need to verify that at least one item meets an async condition.
Import
Section titled “Import”Root package:
import { some } from "@vgerbot/async";Module subpath:
import { some } from "@vgerbot/async/collections";Leaf subpath:
import { some } from "@vgerbot/async/collections/some";Quick example
Section titled “Quick example”import { some } from "@vgerbot/async";
const handle = some( [2, 4, 6, 7], async (item, token) => { await token.sleep(5); return item % 2 === 1; }, { concurrency: 2 },);
const hasOdd = await handle; // trueWhen to use some
Section titled “When to use some”- Existential validation: Check if any item passes an async condition.
- Availability checks: Verify at least one resource is available.
- Early termination: Stop checking as soon as one match is found.
- Permission checks: Check if any user has a specific permission.
// Array inputfunction some<I>( data: I[] | Promise<I[]>, predicate: (item: I, token: CancellableToken) => Promise<boolean>, options?: SomeOptions,): CancellableHandle<boolean>;
// Object inputfunction some<I>( data: CollectionInput<I> | Promise<CollectionInput<I>>, predicate: (item: I, key: number | string, token: CancellableToken) => Promise<boolean>, options?: SomeOptions,): CancellableHandle<boolean>;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 if the item matches. |
options |
SomeOptions |
undefined |
Configuration options. |
SomeOptions
Section titled “SomeOptions”SomeOptions extends CancellableOptions<boolean> with:
| Option | Type | Default | Description |
|---|---|---|---|
concurrency |
number |
Infinity |
Maximum number of concurrent predicate evaluations. |
Return value
Section titled “Return value”Returns a CancellableHandle<boolean> — true if any item passes, false otherwise. Returns false for empty collections.
Execution model
Section titled “Execution model”- Without concurrency limit: Items are checked sequentially. Stops at the first
true. - With concurrency limit: Multiple workers evaluate predicates concurrently. When a
trueis found, new work scheduling stops. Already-running evaluations are allowed to complete.
Error handling
Section titled “Error handling”If any predicate rejects, some 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 always boolean. Empty collections return false.
const hasInvalid = await some(items, async (item) => !item.isValid);if (hasInvalid) { console.warn("Some items are invalid");}