once
This is a function that accepts a function func
and returns a new function that will be called only once. After the first call, it will return the cached result of the first call.
Added in v0.0.2
Usage
typescript
import { once } from 'parsnip-kit'
const handler = (a) => {
console.log('Function Called')
return a
}
const onceFn = once(handler)
onceFn(123)
// Function Called
// result is 123
onceFn(321)
// result is 123
API
Type Parameter
Arg | Type | Description |
---|---|---|
T | extends (...args: any[]) => any | Function type |
Arguments
Arg | Type | Optional | Default | Description |
---|---|---|---|---|
func | T | false | undefined | The function only called once |
Returns
Type |
---|
(...args: Parameters<T>) => ReturnType<T> |