fantasy.result.async

An asynchronous either monad compatible with Folktale result

Usage no npm install needed!

<script type="module">
  import fantasyResultAsync from 'https://cdn.skypack.dev/fantasy.result.async';
</script>

README

Result.Async

This is fantasy land, Folktale, compatible Either monad that can be used for Asynchronous error handling. Is all about using the same good functional pattern of Either along with Promises to deal with asynchronicity without deviating from the functional coding styles. An alternative to data.task and Fluture.js, that builds up on promise familiarity and uses the Either functional pattern. The idea of async Either is the coyoneda lema.

https://medium.com/@dimpapadim3/async-functional-error-handling-9332dfe9f78c

Example use using chaining with Folktale Result.Ok

 const Result = require('folktale/result');
   
   const isDigit = (character) =>
      '0123456789'.split('').includes(character);

   const digit = (input) => {
      const character = input.slice(0, 1);
      const rest = input.slice(1);

      return isDigit(character) ? Result.Ok([character, rest])
         :      /* otherwise */       Result.Error(`Expected a digit (0..9), got "${character}"`);
   };

   Promise.resolve('012').toResult()
      .chain(digit)
      .mergeP()
      .then(console.log)