flow-static-land icon indicating copy to clipboard operation
flow-static-land copied to clipboard

Struggling with Maybe and error handling

Open Dr-Nikson opened this issue 7 years ago • 0 comments

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))

Dr-Nikson avatar Jun 30 '17 13:06 Dr-Nikson