async example
All checks were successful
Build and test / build-and-test (18.x) (push) Successful in 12s
Build and test / build-and-test (20.x) (push) Successful in 12s

This commit is contained in:
James Brumond 2023-08-26 16:01:46 -07:00
parent 13a64c0d2d
commit e5e54ff494
Signed by: james
GPG Key ID: E8F2FC44BAA3357A
2 changed files with 18 additions and 1 deletions

View File

@ -66,3 +66,20 @@ function get_data_from_slow_source() : string {
const get_data_cached = memo_simple(30_000, get_data_from_slow_source);
```
### Full async example
```ts
import { memo_async } from '@js/memo';
async function get_data_from_slow_source(name: string) : Promise<string> {
// ....
}
const get_data_cached = memo_async({
func: get_data_from_slow_source,
ttl: 30_000,
stale_ttl: 5_000,
promise_ttl: 5_000,
});
```

View File

@ -75,7 +75,7 @@ export interface MemoAsyncStoredResult<T extends AsyncFunc> {
timer: ReturnType<typeof setTimeout>;
}
export function memo<T extends AsyncFunc>(opts: MemoAsyncOptions<T>) : T {
export function memo_async<T extends AsyncFunc>(opts: MemoAsyncOptions<T>) : T {
const get_key = opts.key ?? default_key;
const storage: Record<string, MemoAsyncStoredResult<T>> = Object.create(null);
const refreshing: Record<string, MemoAsyncStoredResult<T>> = Object.create(null);