Function: memoizeAsync()
ts
function memoizeAsync<TArgs, TResult>(fn, options?): (...args) => Promise<TResult>;Memoizes an async function, caching results by serialized arguments with optional TTL. 缓存异步函数的结果,支持按参数序列化 key 及可选的 TTL 过期时间。
Type Parameters
| Type Parameter |
|---|
TArgs extends unknown[] |
TResult |
Parameters
| Parameter | Type |
|---|---|
fn | (...args) => Promise<TResult> |
options | MemoizeAsyncOptions |
Returns
(...args) => Promise<TResult>
Example
ts
let calls = 0
const fn = memoizeAsync(async (x) => { calls++; return x * 2 })
await fn(5) // => 10
await fn(5) // => 10 (cached)
calls // => 1Since
1.0.0