API reference@evolu/commonFunction › constant

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

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

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 { assertEqual, assertSame, constant } from "@evolu/common";

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

assertSame(getConstantConfig(), getConstantConfig());
assertEqual(getConstantConfig().version, 1);
assertEqual(getFreshConfig().version, 2);
assertEqual(getFreshConfig().version, 3);