failables
Failables are a clean, consistent, async-friendly, network-friendly result type.
Successful results are represented as failables of type SUCCESS
, with the result stored in the payload.
Empty results are neither success nor failure; they are represented as failables of type EMPTY
, and of course have no payload. Calling success without a payload will result in an empty failable (not a success).
Instead of representing errors sometimes by a result code, sometimes by a magic number, and sometimes by throwing an exception, consistently represent errors as failables of type FAILURE
, with the error message stored in the payload.
That's all there is to it.
usage
const result1 = success('foo')
const result2 = success('foo', { timesCalled: 7 })
const result3 = failure('trouble!')
const result4 = failure('trouble!', { userId: 'fred' })
const result5 = empty()
const result6 = empty({ userId: 'fred' })
const answer = payload(result1)
const metadata = meta(result2)
if (isFailure(result4)) return result4
documentation
creation
success(payload, meta)
failure(payload, meta)
empty(meta)
tests
isFailable(object)
isSuccess(failable)
isFailure(failable)
isEmpty(failable)
elements
payload(failable)
meta(failable)
list operations
anyFailed(list)
firstFailure(list)
extractPayloads(failables)
flattenResults(list)
assertions
assertSuccess(failable, payload)
assertSuccessWhich(predicate, failable)
assertSuccessTyped(type, failable)
assertFailure(failable, payload)
assertEmpty(failable)
utility
hydrate(failable)