Skip to content

Documentation / @frak-labs/core-sdk / index / withCache

Function: withCache()

withCache<TData>(fn, __namedParameters): Promise<TData>

Defined in: utils/cache/withCache.ts:46

Returns the result of a given promise, and caches the result for subsequent invocations against a provided cache key.

Also deduplicates concurrent calls — if multiple callers request the same cache key while the promise is pending, they share the same promise.

Type Parameters

TData

TData

Parameters

fn

() => Promise<TData>

__namedParameters

WithCacheOptions

Returns

Promise<TData>

Example

// First call fetches, subsequent calls return cached data for 30s
const data = await withCache(
    () => client.request({ method: "frak_getMerchantInformation" }),
    { cacheKey: "merchantInfo", cacheTime: 30_000 }
);