flow-static-land
flow-static-land copied to clipboard
Struggling with Maybe and error handling
Hello! I'm pretty new in fp, and I have some some misunderstanding how to resolve cases like this:
const load(/* ... */): Promise<AsyncData> => {
const data: Maybe<Promise<AsyncData>> = maybe.map(
loadAsyncData,
someOtherMaybe
)
return maybe.fromMaybe(
Promise.reject(new Error(/* ... */)), // <-- causes "Uncaught (in promise) Error"
data
)
}
It throws "Uncaught (in promise) Error" to the console every time, because then data is just - I have no chance to handle promise error... It there something like this:
return maybe.fromMaybe(
() => Promise.reject(new Error(/* ... */)), // <-- function instead of value
data
)
For example in scala -
maybe.getOrElse(new Error('...'))
argument will be executed only if maybe is nothing (call-by-name style (x: => Any))