API reference@evolu/commonFunction › constant

function constant<T>(value: T): Thunk<T>;

Defined in: packages/common/src/Function.ts:266

Creates a Thunk that always returns a precomputed value.

Use when the value is expensive to compute and you want to compute it once at definition time rather than on every call.

Example

import { constant } from "@evolu/common";

let version = 0;
const readConfig = () => ({ version: ++version });
const getConstantConfig = constant(readConfig());
const getFreshConfig = () => readConfig();

expect(getConstantConfig()).toBe(getConstantConfig());
expect(getConstantConfig().version).toBe(1);
expect(getFreshConfig().version).toBe(2);
expect(getFreshConfig().version).toBe(3);