Skip to content

always() function

返回一个总是返回给定值的函数。

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.