Skip to main content

parallel

$parallel

Applies an array of processors concurrently to the same input value, returning an array of all results in the same order as the processors.

When all processors are synchronous, execution is sequential and the result is a plain array. When any processor is asynchronous, all are run via Promise.all, giving true concurrent execution — useful for I/O-bound processors such as HTTP fetches or secret lookups.

Errors propagate immediately (fail-fast). Wrap individual processors in $gate or $try to trap errors and return a fallback instead.

Parameters

  • processors (Array, required): Array of processor specifications to run concurrently.

Example

// Fan out to two transforms and collect both results
new Schema('string').transformer({$parallel: ['$uppercase', '$trim']})
// ' hello ' → ['HELLO', 'hello']

// Concurrent async lookups (both execute at the same time via Promise.all)
new Schema('string').transformer({$parallel: [fetchUserFlags, fetchUserPermissions]})
// → [flagsResult, permissionsResult]

// Fan out validation across multiple schemas and collect results
new Schema('object').transformer({
$parallel: [
{$get: 'id'},
{$get: 'name'},
{$get: 'email'},
]
})