function createCachedProbe
Since 0.1.0
createCachedProbe(options: CachedProbeOptions<boolean>): () => Promise<boolean>

Builds a cached, coalesced, time-bounded reachability probe.

The returned function answers the same factual question as options.probetrue when the backend is reachable — but only issues a fresh probe when the cached outcome has aged past ttlMs (measured on the injected monotonic hrtime). Concurrent callers during a single in-flight probe share one probe call. Each probe is bounded by timeoutMs; a probe that exceeds the window, rejects, or throws synchronously resolves false — the attempt never rejects.

Examples

Example 1

const isHealthy = createCachedProbe({
  probe: () => client.ping().then(() => true, () => false),
  hrtime: () => runtime.hrtime(),
});

Parameters

options: CachedProbeOptions<boolean>

Probe, cache TTL, timeout and monotonic clock

Return Type

() => Promise<boolean>

An async function resolving the backend's reachability

createCachedProbe<T>(options: CachedProbeOptions<T> & { readonly fallback: T; }): () => Promise<T>

Widened-outcome form: fallback is REQUIRED.

The default fallback is false, which this function cannot produce for an outcome type that does not include it. Requiring the caller to name theirs is what keeps the returned Promise<T> honest — without this overload a createCachedProbe<'up' | 'down'> call that omitted fallback would hand back false on a timeout under a type promising it could not.

fallback: undefined is a valid value here, not an omission: it is the one the tri-state Service Bus probe passes to mean "could not determine".

Type Parameters

Parameters

options: CachedProbeOptions<T> & { readonly fallback: T; }

Return Type

() => Promise<T>

Usage

import { createCachedProbe } from "common/src/index.ts";