mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
* chore: use descriptive type parameter names * refactor: requested changes --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
19 lines
612 B
TypeScript
19 lines
612 B
TypeScript
/**
|
|
* Lazy is a wrapper around a value that is computed lazily. It is useful for
|
|
* cases where the value is expensive to compute and the computation may not
|
|
* be needed at all.
|
|
*
|
|
* @param cb - The callback to lazily evaluate
|
|
* @typeParam Value - The type of the value
|
|
* @example
|
|
* ```ts
|
|
* const value = lazy(() => computeExpensiveValue());
|
|
* ```
|
|
*/
|
|
// eslint-disable-next-line promise/prefer-await-to-callbacks
|
|
export function lazy<Value>(cb: () => Value): () => Value {
|
|
let defaultValue: Value;
|
|
// eslint-disable-next-line promise/prefer-await-to-callbacks
|
|
return () => (defaultValue ??= cb());
|
|
}
|