Every callback receives a ResilienceContext. Its most important field is signal: timeouts, hedging and caller
cancellation all work by aborting it. JavaScript can’t stop a running promise, so pass signal to your I/O.
Runs the callback through the pipeline; throws the final error.
execute(({
signal: AbortSignal
Aborted when the caller cancels, or when a strategy (timeout, hedging) gives up on this execution.
signal,
attemptNumber: number
0 for the first attempt, then 1, 2, … in the order attempts start. Use it for logging or per-attempt idempotency keys.
One counter per execution numbers every retry and hedged attempt, however retry and hedging are nested
(retry around hedging, retry inside hedged branches), so a number is never repeated within an execution.
Strategies' own counters (RetryPredicateArgs.attemptNumber, OnHedgingArgs.attemptNumber) stay local.
A Headers object, an object literal, or an array of two-item arrays to set request's headers.
headers: { 'x-attempt':
var String:StringConstructor
(value?:any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(
attemptNumber: number
0 for the first attempt, then 1, 2, … in the order attempts start. Use it for logging or per-attempt idempotency keys.
One counter per execution numbers every retry and hedged attempt, however retry and hedging are nested
(retry around hedging, retry inside hedged branches), so a number is never repeated within an execution.
Strategies' own counters (RetryPredicateArgs.attemptNumber, OnHedgingArgs.attemptNumber) stay local.
Aborted when the caller cancels, an outer pipeline gives up, or a strategy (timeout, hedging) abandons this attempt
attemptNumber
number
0 for the first attempt, then 1, 2… for retries and hedged attempts, in the order they start. Unique within an execution, however retry and hedging are nested
operationKey
string | undefined
From execute(fn, { operationKey }); used by filters such as setGlobalChaos({ filter })
properties
Map<string, unknown>
A bag shared by the strategies and the callback for this execution
idempotent
boolean
false forbids repeating calls that may have started (default true)
operationName
string?
Telemetry name for this execution (execute(fn, { name }))
Abort each attempt’s signal as soon as its callback settles
name
pipeline name
Operation name for spans and metrics
attributes
none
Extra low-cardinality attributes for spans and OpenTelemetry metrics
timeProvider
pipeline’s clock
Clock for this run’s retry delays, timeouts, hedging delays and chaos latency
A per-execution timeProvider only affects timing that belongs to the execution. Breakers, limiters, caches and locks
keep the pipeline’s clock, because their state spans executions.
A pipeline executed inside another pipeline’s callback inherits its signal. An outer timeout therefore cancels every
call made underneath it, including calls that never received the signal:
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().
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().
chargeCustomer('c_42')); // the fetch is aborted when the 3 s budget runs out
The nested signal aborts when either its own strategies or any outer execution give up. This works through
AsyncLocalStorage, so it needs a runtime that has it: Node, Bun, Deno, and Workers with nodejs_compat. In
browsers, pass the signal explicitly (see Runtimes).
Context of the resilient execution the calling code runs in (works in nested services), or undefined.
getResilienceContext()?.
ResilienceContext.attemptNumber: number |undefined
0 for the first attempt, then 1, 2, … in the order attempts start. Use it for logging or per-attempt idempotency keys.
One counter per execution numbers every retry and hedged attempt, however retry and hedging are nested
(retry around hedging, retry inside hedged branches), so a number is never repeated within an execution.
Strategies' own counters (RetryPredicateArgs.attemptNumber, OnHedgingArgs.attemptNumber) stay local.
attemptNumber??0;
return
constdb: {
query(sql:string, options: {
signal?:AbortSignal;
}):Promise<unknown[]>;
}
db.
functionquery(sql:string, options: {
signal?:AbortSignal;
}):Promise<unknown[]>
query(`select * from orders where customer_id = $1 -- attempt ${
Aborted when the caller cancels, or when a strategy (timeout, hedging) gives up on this execution.
signal), {
ExecuteOptions.idempotent?: boolean |undefined
Default true. false: never repeat a call that may have reached the dependency (no retries/hedging of started calls).
idempotent: false });
Retry and hedging then only repeat the call when it never started, for example because a limiter rejected it. A failure that may have reached the dependency (a timeout, a 503, a dropped connection) is returned
as is. Hedging never launches a parallel attempt while one may be in flight.
Some callbacks start work that outlives them: a timer, a stream the caller only partly reads, a background refresh.
abortOnReturn: true aborts the attempt’s signal as soon as the callback settles, so that work stops too.
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().
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()).