Promise.all has two problems in resilient code: when one task fails, the others keep running and burn quota for a result nobody will read, and it has no concurrency cap. These helpers fix both. Each task runs through an optional pipeline on its own (its own retries and timeout), receives a context with a signal, and is cancelled when the batch gives up. waitFor covers the other common loop: poll until something is ready.
They are plain functions, not pipeline strategies. To merge many calls into one request (DataLoader-style), use the batcher instead.
Runs tasks in parallel. On the first failure, the others are aborted through their context signal, and all rejects with that failure. The result is a typed tuple.
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Each task / item runs through it separately (e.g. its own retries and timeout).
pipeline,
BatchOptions.signal?: AbortSignal |undefined
signal:
var AbortSignal: {
new ():AbortSignal;
prototype:AbortSignal;
abort(reason?:any):AbortSignal;
any(signals:AbortSignal[]):AbortSignal;
timeout(milliseconds:number):AbortSignal;
}
The AbortSignal interface represents a signal object that allows you to communicate with an asynchronous operation (such as a fetch request) and abort it if required via an AbortController object.
Runs every task to the end and returns one Outcome per task, in order. It never rejects, except when the caller’s signal aborts (it then rejects with the abort reason once the tasks have settled).
Aborted when the caller cancels, or when a strategy (timeout, hedging) gives up on this execution.
signal)]);
for (const
consto:Outcome<number>
oof
constoutcomes: [Outcome<number>, Outcome<number>]
outcomes)
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Polly-style builder with a shortcut for every strategy: addRetry(), addTimeout(), addCircuitBreaker(), …
Convenient, but it bundles every strategy. For the smallest browser bundle use PipelineBuilder with
retryStrategy(), timeoutStrategy(), … instead; the two are interchangeable.
The type parameters track which errors the pipeline can raise, for pipeline.settle() + Outcome.match().
Returns the elements of an array that meet the condition specified in a callback function.
@param ― predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
filter((
r: Outcome<string>
r) =>!
r: Outcome<string>
r.
ok: boolean
ok).
Array<{ readonly ok: false; readonly error: unknown; }>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length;
With stopOnError: false the return type is Outcome<R>[]; otherwise R[].
Polls condition every interval until it returns true. Rejects with TimeoutRejectedError after timeout,
and with the error if condition throws (wrap it in .catch(() => false) to treat errors as "not yet").
The ok read-only property of the Response interface contains a Boolean stating whether the response was successful (status in the range 200-299) or not.
Polls condition every interval until it returns true. Rejects with TimeoutRejectedError after timeout,
and with the error if condition throws (wrap it in .catch(() => false) to treat errors as "not yet").
Pause between checks: fixed, or per poll (e.g. backoff: (n) => Math.min(100 * 2 ** n, 5000)).
interval: (
n: number
n) =>
var Math:Math
An intrinsic object that provides basic mathematics functionality and constants.
Math.
Math.min(...values: number[]): number
Returns the smaller of a set of supplied numeric expressions.
@param ― values Numeric expressions to be evaluated.
min(100*2**
n: number
n, 5000),
WaitForOptions.timeout: Duration
Rejects with TimeoutRejectedError after this long.
timeout: '10m' },
);
The first check runs immediately. Between checks it pauses interval, or interval(n) where n is 1 before the second check, 2 before the third, and so on. The backoff helpers produce such functions (interval: backoff.exponential({ delay: '100ms', maxDelay: '5s' })), but they count from 0, so the first pause is their second step: 200 ms here, and backoff.sequence(['100ms', '1s']) starts at 1 s.
Option
Type
Default
Description
interval
Duration | (attempt: number) => Duration
required
Pause between checks: fixed, or per pause. ≥ 0.
timeout
Duration
required
Give up after this long with TimeoutRejectedError. > 0.
signal
AbortSignal
–
Cancels waiting; rejects with its reason.
timeProvider
TimeProvider
system clock
Clock for the interval and timeout, e.g. FakeTimeProvider in tests.
condition gets a signal that aborts on timeout or cancellation. If it ignores the signal, waitFor still stops waiting for it at the deadline. A throwing condition ends the wait; to treat errors as “not yet”, catch them: (signal) => check(signal).catch(() => false).