mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
231b7ef83c
* feat(after): Add after function * feat(after): Add after test code * feat(after): Add after benchmark * feat(after): Add after docs * fix(after): should create a function that unc only after being called calls * docs(after): change the after function docs * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: Sojin Park <raon0211@gmail.com> Co-authored-by: Sojin Park <raon0211@toss.im>
48 lines
1.0 KiB
Markdown
48 lines
1.0 KiB
Markdown
# after
|
|
|
|
Creates a function that only executes starting from the `n`-th call.
|
|
The provided function will be invoked starting from the `n`-th call.
|
|
|
|
The is particularly useful for scenarios involving events or asynchronous operations where an action should occur only after a certain number of invocations.
|
|
|
|
## Signature
|
|
|
|
```typescript
|
|
function after<F extends (...args: any[]) => any>(n: number, func: F): F;
|
|
```
|
|
|
|
### Parameters
|
|
|
|
- `n` (`number`): The number of calls required for `func` to execute.
|
|
- `func` (`F`): The function to be invoked.
|
|
|
|
### Returns
|
|
|
|
(`F`): A new function that:
|
|
|
|
- Tracks the number of calls.
|
|
- Invokes `func` starting from the `n`-th call.
|
|
- Returns `undefined` if fewer than `n` calls have been made.
|
|
|
|
### Throws
|
|
|
|
Throws an error if `n` is negative.
|
|
|
|
## Examples
|
|
|
|
```typescript
|
|
import { after } from 'es-toolkit/function';
|
|
|
|
const mockFn = () => {
|
|
console.log('called');
|
|
};
|
|
const afterFn = after(3, mockFn);
|
|
|
|
// Will not log anything.
|
|
afterFn();
|
|
// Will not log anything.
|
|
afterFn();
|
|
// Will log 'called'.
|
|
afterFn();
|
|
```
|