API reference › @evolu/common › Function › Thunk
type Thunk<T> = () => T;
Defined in: packages/common/src/Function.ts:241
A function that takes no arguments and returns a value.
Useful for:
- Providing default callbacks (see constVoid, constTrue, etc.)
- Delaying expensive operations until actually needed
- Deferring side effects so the callee controls when they run
Example
import { constVoid, type Thunk } from "@evolu/common";
const notify = (onDone: Thunk<void> = constVoid) => onDone();
notify();
let value = 0;
const compute: Thunk<number> = () => ++value;
const jobs: Array<Thunk<void>> = [];
const schedule = (job: Thunk<void>): void => {
jobs.push(job);
};
schedule(() => {
value += 10;
});
const computed = compute();
jobs.shift()?.();
expect(computed).toBe(1);
expect(value).toBe(11);