rich-domain
rich-domain copied to clipboard
Chained results of healthy mind
Is your feature request related to a problem? Please describe. Chained Results are hard to read in actual codebases.
Describe the solution you'd like
We could take inspiration from neverthrow library which is inspired by Javascript Promise API.
Add non awaitable .then and .catch alternatives to Result type.
Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.
Additional context aka Examples Current state:
const hugeAggregateCreated = HugeAggregate.create(bigProps);
if (hugeAggregateCreated.isFail()) { /* impl */ }
const hugeAggregate = hugeAggregateCreated.value();
const moneyDone = hugeAggregate.doMoney();
if (moneyDone.isFail()) { /* impl */ }
const money = moneyDone.value();
const moneySmoked = money.smoke();
if (moneySmoken.isFail()) { /* impl */ }
// ...
I have also considered this case, but it does not seem typesafe to me at all. And there is still an edge case, when .combine is too big.
const hugeAggregateCreated = HugeAggregate.create(bigProps);
const moneyDone = hugeAggregateCreated.value().doMoney();
const moneySmoked = moneyDone.value().smoke(); // what if it worked?
// ...
if (Result.combine([hugeAggregateCreated, moneyDone, ..., moneySmoked]).isFail()) {
return Result.fail()
}
const businessThing = /* from some chain output */
const profitExtracted = businessGuy.extractProfit(businessThing);
if (profitExtracted.isFail()) {
businessGuy.dontExtractProfit()
}
Proposed:
const businessThing = hugeAggregate.create(bigProps)
.andThen((aggregate) => aggregate.doMoney())
.andThen((money) => money.smoke) // does nothing if callback item is error
// ...
.orElse((error) => /* error handling impl */)
businessGuy.extractProfit(businessThing)
.orElse(businessGuy.dontExtractProfit)