Skip to content

falsyPromiseOrThrow

Generates a .then() interceptor that avoids any truthy value by throwing an error.

Falsy values: false, 0, -0, 0n, "", null, undefined, and NaN.

Available since v1.1.0

import { falsyPromiseOrThrow } from '@helpers4/promise';
falsyPromiseOrThrow<T>(error: string): (data: T) => T | never
ParameterTypeDescription
errorstringThe error message thrown if the data is truthy.

(data: T) => T | never — A .then() interceptor: returns data unchanged, or throws error.

import { falsyPromiseOrThrow } from '@helpers4/promise';

Promise.resolve('good value')
  .then(falsyPromiseOrThrow('My custom error message'))
  .then(console.log);
// => 'My custom error message'

View on npm