Skip to content

falsyPromiseOrThrow

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

Available since v1.0.0

import { falsyPromiseOrThrow } from '@helpers4/promise';
falsyPromiseOrThrow<T>(error: string): function
ParameterTypeDescription
errorstringThe error message to throw if data is truthy

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

Returns the value if falsy, throws otherwise.

Promise.resolve(null).then(falsyPromiseOrThrow('Expected falsy'))
// => null

Throws an error when the value is truthy.

Promise.resolve('oops').then(falsyPromiseOrThrow('Should be empty'))
// throws Error('Should be empty')

View source on GitHub