Skip to content

always

Create a function that always returns the same value

47 bytes

Usage

Creates a function that always returns the same value, regardless of any arguments passed to it.

import * as _ from 'radashi'
const alwaysTrue = _.always(true)
alwaysTrue() // true
alwaysTrue(1, 2, 3) // true

Use cases

You can avoid using always if the value is a primitive (use () => true instead), but it can be useful if you need a function that always returns the same object reference, or if you want to memoize a calculation across multiple calls.

// Not memoized
() => someCalculation()
// Memoized
_.always(someCalculation())
// Not same object
() => ({ a: 1, b: 2 })
// Same object
_.always({ a: 1, b: 2 })