Skip to main content

withFallback()

withFallback<In, Out>(primary, fallback): Strategy<In, Out>

Composes a primary strategy with a fallback. If the primary throws, the fallback is executed instead.


Type Parameters​

In: In​

The input type

Out: Out​

The output type


Parameters​

primary: Strategy<In, Out>​

The strategy to try first

fallback: Strategy<In, Out>​

The strategy to use if primary throws


Returns: Strategy<In, Out>​

A new combined strategy


Since​

2.4.0


Example​

const fromCache: `Strategy<string, Data>` = (key) => cache.get(key);
const fromApi: `Strategy<string, Data>` = (key) => api.fetch(key);

const fetchData = withFallback(fromCache, fromApi);
fetchData("user:123"); // tries cache, falls back to API