Collections
Collection helpers apply an async function to each element of an array or object. They support optional concurrency limits and cancellation.
Import
Section titled “Import”import { map, filter, reduce, each } from "@vgerbot/async";import { map, filter, reduce } from "@vgerbot/async/collections";Array vs object input
Section titled “Array vs object input”All collection APIs accept both arrays and objects. For arrays, the callback receives (item, token). For objects, the callback receives (item, key, token).
// Arraymap([1, 2, 3], async (n, token) => n * 2);
// Objectmap({ a: 1, b: 2 }, async (v, k, token) => v * 2);Concurrency
Section titled “Concurrency”Most collection APIs accept a concurrency option to limit simultaneous operations:
map(items, async (item, token) => fetch(item.url), { concurrency: 5 });| API | Description |
|---|---|
map |
Transform each item, returning an array of results. |
each |
Iterate for side effects, no results collected. |
filter |
Keep items that pass an async predicate. |
reduce |
Sequentially accumulate a result. |
detect |
Find the first item matching a predicate. |
every |
Check if all items pass a predicate. |
some |
Check if any item passes a predicate. |
groupBy |
Group items by an async-computed key. |
sortBy |
Sort items by an async-computed key. |
concat |
Map and flatten results (flatMap). |
partition |
Split into two arrays by predicate. |
mapValues |
Transform object values, preserving keys. |
pick |
Filter object properties by predicate. |
omit |
Exclude object properties by predicate. |
transform |
Build a result with in-place mutation. |
reject |
Remove items matching a predicate. |
findIndex |
Find the index of the first matching item. |