UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

52 lines (51 loc) 1.83 kB
/** * @module Async */ import { type AsyncMiddlewareFn, type HookContext } from "../../../../utilities/_module-exports.js"; import type { HedgingSettings } from "../../../../async/middlewares/hedging/_shared.js"; /** * The `concurrentHedging` middleware executes the primary function and all fallback functions concurrently. * It returns the result of the first successful function and automatically aborts all remaining functions. * If all function fail than error is thrown. * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group Middlewares * @throws {HedgingAsyncError} {@link HedgingAsyncError} * * @example * ```ts * import { concurrentHedging } from "@daiso-tech/core/async"; * import { AsyncHooks } from "@daiso-tech/core/utilities"; * * async function fn1(signal?: AbortSignal): Promise<unknown> { * const response = await fetch("ENDPOINT-1", { signal }); * return await response.json(); * } * async function fn2(signal?: AbortSignal): Promise<unknown> { * const response = await fetch("ENDPOINT-2", { signal }); * return await response.json(); * } * async function fn3(signal?: AbortSignal): Promise<unknown> { * const response = await fetch("ENDPOINT-3", { signal }); * return await response.json(); * } * const fetchData = new AsyncHooks(fn1, [ * concurrentHedging({ * fallbacks: [ * fn2, * fn3 * ] * }) * ], { * signalBinder: { * getSignal: (args) => args[0], * forwardSignal: (args, signal) => { * args[0] = signal; * } * } * }); * * console.log(await fetchData.invoke()); * ``` */ export declare function concurrentHedging<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings: NoInfer<HedgingSettings<TParameters, TReturn, TContext>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;