1. 32

  2. 7

    https://github.com/uniphil/results is a library for that lets you use ADTs like this inside JS, it’s kind of fantastic!

    1. 7

      Results author here, thanks for mentioning it, @maxbittker!

      We use Results pretty heavily in our 50k JS client app at OpenRide, for exactly the kinds of cases mentioned in the article. It’s not magic, and there are hard things we’re still figuring out, especially around modeling async data.

      In particular, we found this type,

      type RemoteData e a
          = NotAsked
          | Loading
          | Failure e
          | Success a

      fairly troublesome: NotAsked is just hard. It’s super-annoying to have to handle that case in component views, and if we don’t have bugs, components should just never be rendered with a NotAsked. We settled on

      { Done, Pending, Errored }

      where Errored is a subsequent ADT/Union/whatever of RequestError, which includes NotStarted.

      This is all cool, and can extended to model optimistic updates, but it’s easy to let it get very messy, miss cases, and confuse data-modeling requirements for stuff we give to view components vs. requirements for the client storage, which we’ve found are quite distinct.