Skip to content

truthyPromiseOrThrow

Returns a function that passes through truthy data or throws an error.

Available since v1.0.0

import { truthyPromiseOrThrow } from '@helpers4/promise';
// or, from the all-in-one package (same code, one install):
import { truthyPromiseOrThrow } from 'helpers4/promise';
truthyPromiseOrThrow<T>(error: string): function
ParameterTypeDescription
errorstringThe error message to throw if data is falsy

function — A function that returns the data if truthy, or throws

Returns the value if truthy, throws otherwise.

Promise.resolve('data').then(truthyPromiseOrThrow('No data'))
// => 'data'

Throws an error when the value is falsy.

Promise.resolve('').then(truthyPromiseOrThrow('Empty!'))
// throws Error('Empty!')

View source on GitHub