Skip to content

meaningPromiseOrThrow

Returns a function that passes through meaningful data or throws an error. Data is considered meaningless if it is null, undefined, empty string, empty object, or empty array.

Available since v1.0.0

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

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

Returns the value if it is not empty (null, undefined, empty string, empty object, empty array).

Promise.resolve({ key: 'value' }).then(meaningPromiseOrThrow('No data'))
// => { key: 'value' }

Throws when the value is null, undefined, empty string, empty object, or empty array.

Promise.resolve({}).then(meaningPromiseOrThrow('Empty!'))
// throws Error('Empty!')

View source on GitHub