One-off timeout: await withTimeout(({ signal }) => fetch(url, { signal }), '2s'). Rejects with TimeoutRejectedError.
withTimeout,
classNonRetryableError
Throw (or wrap an error in) this to stop retrying right away: retry gives up and the caller receives the original error.
Like p-retry's AbortError. throw new NonRetryableError(error) or throw new NonRetryableError('invalid card').
Throw new NonRetryableError(err) inside the callback to stop retrying at once; the caller receives err.
The defaults already skip what retrying can’t fix: 4xx client errors (except 408 and 429) and aborts.
See What counts as a failure.
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().
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.
Represents the completion of an asynchronous operation
Promise<{
id: string
id:string;
total: number
total:number }>;
});
}
What this does:
Order matters. The first strategy added is the outermost. Retry wraps the breaker, which wraps the timeout,
so each attempt gets 2 s and is counted by the breaker. Reversing the order changes the behaviour; see
Pipelines and strategy order.
HttpError has a status. The default predicates read it: a 503 is retried and counted by the breaker, a 404
is neither.
signal is passed to fetch. The timeout works by aborting it. Without it, the request keeps running after
the pipeline has given up.
execute() throws. settle() returns an outcome whose error type lists everything the pipeline can raise, and
Outcome.match() requires a handler for each error code:
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().
match<T, E, HextendsOutcomeHandlers<T, E, unknown>>(outcome:Outcome<T, E>, handlers:H):ReturnType<H[keyofH] & ((...args:any) =>any)>;
}
The result of an execution: either a value or an error. Strategies pass outcomes around instead of throwing.
E is the error type when it's known (see pipeline.settle()), unknown otherwise.
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().
CircuitBreakerOptions<any>.consecutiveFailures?: number |undefined
Break after this many failures in a row (a success resets the count). Simple and good for low traffic.
When set without failureRatio, the ratio check is off and only this rule applies.
Like executeOutcome, but the error is typed: the errors this pipeline's strategies can raise (E), the errors your
callback returns with Outcome.fromError(…) (E2), or UnexpectedError wrapping anything else (a thrown bug, a
client error). Pair it with Outcome.match() for exhaustive handling:
constoutcome=await pipeline.settle(async () => (user ? user : Outcome.fromError(newNotFound(id))));
match<T, E, HextendsOutcomeHandlers<T, E, unknown>>(outcome:Outcome<T, E>, handlers:H):ReturnType<H[keyofH] & ((...args:any) =>any)>;
}
The result of an execution: either a value or an error. Strategies pass outcomes around instead of throwing.
E is the error type when it's known (see pipeline.settle()), unknown otherwise.
match<T, E, HextendsOutcomeHandlers<T, E, unknown>>(outcome:Outcome<T, E>, handlers:H):ReturnType<H[keyofH] & ((...args:any) =>any)>;
}
The result of an execution: either a value or an error. Strategies pass outcomes around instead of throwing.
E is the error type when it's known (see pipeline.settle()), unknown otherwise.
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().
Awaits the asyncFn promise or, if asyncFn is a function, immediately
calls the function and awaits the returned promise to complete. It will then
check that the promise is rejected.
If asyncFn is a function and it throws an error synchronously, assert.rejects() will return a rejected Promise with that error. If the
function does not return a promise, assert.rejects() will return a rejected Promise with an ERR_INVALID_RETURN_VALUE
error. In both cases the error handler is skipped.
Besides the async nature to await the completion behaves identically to
throws
.
If specified, error can be a Class,
RegExp, a validation function,
an object where each property will be tested for, or an instance of error where
each property will be tested for including the non-enumerable message and name properties.
If specified, message will be the message provided by the `
AssertionError
if theasyncFn` fails to reject.
import assert from'node:assert/strict';
await assert.rejects(
async () => {
thrownewTypeError('Wrong value');
},
{
name: 'TypeError',
message: 'Wrong value',
},
);
import assert from'node:assert/strict';
await assert.rejects(
async () => {
thrownewTypeError('Wrong value');
},
(err) => {
assert.strictEqual(err.name, 'TypeError');
assert.strictEqual(err.message, 'Wrong value');
returntrue;
},
);
import assert from'node:assert/strict';
assert.rejects(
Promise.reject(newError('Wrong value')),
Error,
).then(() => {
// ...
});
error cannot be a string. If a string is provided as the second argument, then error is assumed to
be omitted and the string will be used for message instead. This can lead to easy-to-miss mistakes. Please read the
example in
throws
carefully if using a string as the second argument gets considered.
Runs the callback through the pipeline; throws the final error.
execute(() =>new
var Promise:PromiseConstructor
new <unknown>(executor: (resolve: (value:unknown) =>void, reject: (reason?:any) =>void) =>void) =>Promise<unknown>
Creates a new Promise.
@param ― executor A callback used to initialize the promise. This callback is passed two arguments:
a resolve callback used to resolve the promise with a value or the result of another promise,
and a reject callback used to reject the promise with a provided reason or error.