DEV Community

Discussion on: Why async code is so damn confusing (and a how to make it easy)

Collapse
 
yurifrl profile image
Yuri

Great article, I stubble with the same issues a while back, and started using github.com/fluture-js/Fluture it basically transforms promises into monads, something like:

const Future = require('fluture')
const { compose, map } = require('ramda')

const greet = name => Future.of(`Hello ${name}`)
const exclaim = line => Future.of(`${line}!`)
const trace = x => console.log(x)

// With function composition (Ramda)
const sayHello = compose(
  map(trace),
  map(greet),
  exclaim
)
sayHello("bob").fork(console.error, console.log)

I convert all my promises to futures, if it fails instead of catching the error, the map will not be triggered and I can use mapRej to deal with the error

Collapse
 
joelnet profile image
JavaScript Joel

fluture is one of those project I have read about, always wanted to use and have never used. I need to block out some time and just use it one day. I feel like even if I don't end up using it I will learn a lot.