Batch Processing
Execute multiple operations independently.
If you need to execute a list of operations and collect all their results, without letting a single failure stop the entire batch, you can use the batch utilities.
Use catchErrorAll for Promises or functions that return direct values or Promises. It runs the inputs concurrently and returns a corresponding tuple of Result objects.
batch-async.ts
import { catchErrorAll } from '@catcherjs/core';
async function processTasks() {
const tasks = [
Promise.resolve('Task 1 complete'),
Promise.reject(new Error('Task 2 failed')),
Promise.resolve('Task 3 complete')
];
const results = await catchErrorAll(tasks);
results.forEach(result => {
if (result.isOk()) {
console.log(result.data);
} else {
console.error(result.error instanceof Error ? result.error.message : result.error);
}
});
}Use catchErrorAllSync for an array of synchronous functions. It executes them sequentially and returns an array of Result objects.
batch-sync.ts
import { catchErrorAllSync } from '@catcherjs/core';
function parseMultiple() {
const inputs = [
() => JSON.parse('{"a": 1}'),
() => JSON.parse('invalid'),
() => JSON.parse('{"c": 3}')
];
const results = catchErrorAllSync(inputs);
const successfulParses = results
.filter(r => r.isOk())
.map(r => r.data);
console.log(successfulParses);
}Component props
Prop
Type
Default
Name
inputsDescription
An array of Promises, tuples or configuration objects.
Type
CatchErrorAllInput[]Default
-
Name
returnsDescription
A mapped tuple preserving per-input Result types with inferred value and error.
Type
Promise<{ -readonly [K in keyof T]: Result<InferInput<T[K]>, InferInputError<T[K]>> }>Default
-
Component props
Prop
Type
Default
Name
inputsDescription
An array of functions, tuples or configuration objects.
Type
CatchErrorAllSyncInput[]Default
-
Name
returnsDescription
An array of Result objects corresponding to the inputs.
Type
Result<T, E>[]Default
-