mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 10:03:31 +01:00
* feat: add @discordjs/util * fix: builders test * refactor: make rest use lazy for ESM import * chore: make requested changes * Apply suggestions from code review Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com> Co-authored-by: A. Román <kyradiscord@gmail.com> * chore: make requested changes and add tests * chore: regen lockfile * test: add type tests * chore: push missing files * chore: make requested changes * chore: update CI stuff * chore: fix lockfile * chore: make requested changes Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com> Co-authored-by: A. Román <kyradiscord@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
19 lines
592 B
TypeScript
19 lines
592 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 T - The type of the value
|
|
* @example
|
|
* ```ts
|
|
* const value = lazy(() => computeExpensiveValue());
|
|
* ```
|
|
*/
|
|
// eslint-disable-next-line promise/prefer-await-to-callbacks
|
|
export function lazy<T>(cb: () => T): () => T {
|
|
let defaultValue: T;
|
|
// eslint-disable-next-line promise/prefer-await-to-callbacks
|
|
return () => (defaultValue ??= cb());
|
|
}
|