lazy()
lazy<
In,Out>(factory): (input) =>Out
Lazy proxy - defers function creation until first call. The factory is called once on the first invocation, then the created function is used directly for all subsequent calls.
Type Parameters
In: In
The input type
Out: Out
The output type
Parameters
factory: () => (input) => Out
A function that creates the real function on demand
Returns
A proxy that initializes lazily
Since
2.4.0
Example
// Expensive connection is only created when first needed
const query = lazy(() => {
const conn = createConnection(); // heavy init
return (sql: string) => conn.execute(sql);
});
// Nothing happens yet...
query("SELECT 1"); // NOW the connection is created, then query runs
query("SELECT 2"); // reuses the same connection