every
every checks whether every item in a collection passes an async predicate. It short-circuits scheduling when the first false is found.
Best for universal validation Use
everywhen you need to verify that all items meet an async condition.
Import
Section titled “Import”Root package:
import { every } from "@vgerbot/async";Module subpath:
import { every } from "@vgerbot/async/collections";Leaf subpath:
import { every } from "@vgerbot/async/collections/every";Quick example
Section titled “Quick example”import { every } from "@vgerbot/async";
const handle = every( [2, 4, 6], async (item, token) => { await token.sleep(5); return item % 2 === 0; }, { concurrency: 2 },);
const allEven = await handle; // trueWhen to use every
Section titled “When to use every”- Validation: Verify all items pass an async check (e.g., all URLs are reachable).
- Precondition checks: Ensure all conditions are met before proceeding.
- Quality checks: Validate all items in a batch.
- Short-circuit optimization: Stop checking as soon as one item fails.
// Array inputfunction every<I>( data: I[] | Promise<I[]>, predicate: (item: I, token: CancellableToken) => Promise<boolean>, options?: EveryOptions,): CancellableHandle<boolean>;
// Object inputfunction every<I>( data: CollectionInput<I> | Promise<CollectionInput<I>>, predicate: (item: I, key: number | string, token: CancellableToken) => Promise<boolean>, options?: EveryOptions,): 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 passes. |
options |
EveryOptions |
undefined |
Configuration options. |
EveryOptions
Section titled “EveryOptions”EveryOptions 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 all items pass, false otherwise. Returns true for empty collections.
Execution model
Section titled “Execution model”- Without concurrency limit: Items are checked sequentially. Stops at the first
false. - With concurrency limit: Multiple workers evaluate predicates concurrently. When a
falseis found, new work scheduling stops. Already-running evaluations are allowed to complete.
Error handling
Section titled “Error handling”If any predicate rejects, every 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 true.
const allValid = await every(items, async (item) => item.isValid);if (allValid) { // proceed}Related APIs
Section titled “Related APIs”somechecks if any item matches.detectfinds the first matching item.filtercollects all matching items.allSettledcollects all outcomes from tasks.