Skip to content

always() function

Returns a function that always returns the given value.

Signature:

typescript
declare function always<T>(value: T): (...args: any[]) => T;

Example

ts
const alwaysFive = always(5);
alwaysFive(); // 5

const defaultToEmpty = always("");
await Promise.reject("error during operation").catch(defaultToEmpty); // ""

// with pipe
const excludeNegatives = false;
pipe(
 [-1, 1, 2, 0 ,-3],
 filter(excludeNegatives ? lt(0) : always(true)),
 toArray,
); // [-1, 1, 2, 0 ,-3]

Open Source Code

Released under the Apache-2.0 License.