gcanti / flow-static-land

[DEPRECATED, please check out fp-ts] Implementation of common algebraic types in JavaScript + Flow
MIT License
408 stars 22 forks source link

Struggling with Maybe and error handling #60

Open Dr-Nikson opened 7 years ago

Dr-Nikson commented 7 years ago

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