|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Rev | Line | |
|---|
| [a762898] | 1 | /**
|
|---|
| 2 | * Creates a function that only executes starting from the `n`-th call.
|
|---|
| 3 | * The provided function will be invoked starting from the `n`-th call.
|
|---|
| 4 | *
|
|---|
| 5 | * This is particularly useful for scenarios involving events or asynchronous operations
|
|---|
| 6 | * where an action should occur only after a certain number of invocations.
|
|---|
| 7 | *
|
|---|
| 8 | * @template F - The type of the function to be invoked.
|
|---|
| 9 | * @param {number} n - The number of calls required for `func` to execute.
|
|---|
| 10 | * @param {F} func - The function to be invoked.
|
|---|
| 11 | * @returns {(...args: Parameters<F>) => ReturnType<F> | undefined} - A new function that:
|
|---|
| 12 | * - Tracks the number of calls.
|
|---|
| 13 | * - Invokes `func` starting from the `n`-th call.
|
|---|
| 14 | * - Returns `undefined` if fewer than `n` calls have been made.
|
|---|
| 15 | * @throws {Error} - Throws an error if `n` is negative.
|
|---|
| 16 | * @example
|
|---|
| 17 | *
|
|---|
| 18 | * const afterFn = after(3, () => {
|
|---|
| 19 | * console.log("called")
|
|---|
| 20 | * });
|
|---|
| 21 | *
|
|---|
| 22 | * // Will not log anything.
|
|---|
| 23 | * afterFn()
|
|---|
| 24 | * // Will not log anything.
|
|---|
| 25 | * afterFn()
|
|---|
| 26 | * // Will log 'called'.
|
|---|
| 27 | * afterFn()
|
|---|
| 28 | */
|
|---|
| 29 | declare function after<F extends (...args: any[]) => any>(n: number, func: F): (...args: Parameters<F>) => ReturnType<F> | undefined;
|
|---|
| 30 |
|
|---|
| 31 | export { after };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.