Skip to content

once

Create a function that runs at most once

169 bytes

Usage

Create a wrapper around a given function such that it executes at most once. Subsequent calls to the wrapped function return the result from the first execution, regardless of the arguments provided. This behavior is akin to memoization but specifically designed for single-use functions. The result of the first call is stored internally, allowing for efficient retrieval without recomputation.

import * as
import _
_
from 'radashi'
const
const fn: (this: unknown) => number
fn
=
import _
_
.
once<[], number, unknown>(fn: (this: unknown) => number): (this: unknown) => number
export once

Create a function that runs at most once, no matter how many times it's called. If it was already called before, returns the result from the first call. This is a lighter version of memo().

To allow your once-wrapped function to be called again, see the once.reset function.

@seehttps://radashi.js.org/reference/curry/once

@example

const fn = once(() => Math.random())
fn() // 0.5
fn() // 0.5

once
(() =>
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.random(): number

Returns a pseudorandom number between 0 and 1.

random
())
const fn: (this: unknown) => number
fn
() // 0.5
const fn: (this: unknown) => number
fn
() // 0.5

Resetting the function

The once.reset function clears the stored result of a function that was previously wrapped with once. This allows the function to be executed again as if it were never called before, enabling dynamic reuse of the function with fresh computations.

import * as
import _
_
from 'radashi'
const
const fn: (this: unknown) => number
fn
=
import _
_
.
once<[], number, unknown>(fn: (this: unknown) => number): (this: unknown) => number
export once

Create a function that runs at most once, no matter how many times it's called. If it was already called before, returns the result from the first call. This is a lighter version of memo().

To allow your once-wrapped function to be called again, see the once.reset function.

@seehttps://radashi.js.org/reference/curry/once

@example

const fn = once(() => Math.random())
fn() // 0.5
fn() // 0.5

once
(() =>
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.random(): number

Returns a pseudorandom number between 0 and 1.

random
())
const fn: (this: unknown) => number
fn
() // 0.5
const fn: (this: unknown) => number
fn
() // 0.5
import _
_
.
const once: Once
export once

Create a function that runs at most once, no matter how many times it's called. If it was already called before, returns the result from the first call. This is a lighter version of memo().

To allow your once-wrapped function to be called again, see the once.reset function.

@seehttps://radashi.js.org/reference/curry/once

@example

const fn = once(() => Math.random())
fn() // 0.5
fn() // 0.5

once
.
function reset(fn: _.OnceFunction): void

Reset the result of a function that was created with once, allowing it to be called again.

const fn = once(() => Math.random())
fn() // 0.5
fn() // 0.5
once.reset(fn)
fn() // 0.3
fn() // 0.3

reset
(
const fn: (this: unknown) => number
fn
)
const fn: (this: unknown) => number
fn
() // 0.3
const fn: (this: unknown) => number
fn
() // 0.3