generated from templates/typescript-library
async example
This commit is contained in:
parent
13a64c0d2d
commit
e5e54ff494
17
readme.md
17
readme.md
@ -66,3 +66,20 @@ function get_data_from_slow_source() : string {
|
|||||||
|
|
||||||
const get_data_cached = memo_simple(30_000, get_data_from_slow_source);
|
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,
|
||||||
|
});
|
||||||
|
```
|
||||||
|
@ -75,7 +75,7 @@ export interface MemoAsyncStoredResult<T extends AsyncFunc> {
|
|||||||
timer: ReturnType<typeof setTimeout>;
|
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 get_key = opts.key ?? default_key;
|
||||||
const storage: Record<string, MemoAsyncStoredResult<T>> = Object.create(null);
|
const storage: Record<string, MemoAsyncStoredResult<T>> = Object.create(null);
|
||||||
const refreshing: Record<string, MemoAsyncStoredResult<T>> = Object.create(null);
|
const refreshing: Record<string, MemoAsyncStoredResult<T>> = Object.create(null);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user