UNPKG

cockatiel

Version:

A resilience and transient-fault-handling library that allows developers to express policies such as Backoff, Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. Inspired by .NET Polly.

28 lines 804 B
import { neverAbortedSignal } from './common/abort'; export class FallbackPolicy { constructor(executor, value) { this.executor = executor; this.value = value; /** * @inheritdoc */ this.onSuccess = this.executor.onSuccess; /** * @inheritdoc */ this.onFailure = this.executor.onFailure; } /** * Executes the given function. * @param fn Function to execute. * @returns The function result or fallback value. */ async execute(fn, signal = neverAbortedSignal) { const result = await this.executor.invoke(fn, { signal }); if ('success' in result) { return result.success; } return this.value(); } } //# sourceMappingURL=FallbackPolicy.js.map