Skip to content

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

ArgTypeDescription
Textends (...args: any[]) => anyFunction type

Arguments

ArgTypeOptionalDefaultDescription
funcTfalseundefinedThe function only called once

Returns

Type
(...args: Parameters<T>) => ReturnType<T>