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