Skip to content

Instantly share code, notes, and snippets.

@i-am-tom
Created November 21, 2017 12:12
Show Gist options
  • Save i-am-tom/71e9bb3efdc3adc74f0735c009218269 to your computer and use it in GitHub Desktop.
Save i-am-tom/71e9bb3efdc3adc74f0735c009218269 to your computer and use it in GitHub Desktop.
Config-dependent Future.
const Future = require('fluture')
// I can't guarantee this will work out of the box - it's quite old... but
// you should hopefully be able to see how this fits together!
const { ReaderT } = require('fantasy-readers')
// First of all, you're going to need to "lift" all your `Future` operations
// to work within ReaderT. Basically, you just need to call `ReaderT.lift` on
// any `Future` values.
const doAjaxThing_ = x => ReaderT.lift(doAjaxThing(x))
const doOtherThing_ = x => y => ReaderT.lift(doOtherThing(x)(y))
// A little app that calls `doOtherThing_` with the result of `doAjaxThing_` and
// your global config. It's "ReaderT Config (Future Error Result)"
const app =
doAjaxThing_
.chain(stuff =>
ReaderT.ask // Here, we just "ask" for the config!
.chain(config =>
doOtherThing_(stuff)(config)))
// Future Error Result (production app)
const appOnProduction = app.run(prodConfig)
// Future Error Result (development app)
const appOnDevelopment = app.run(devConfig)
// Run as usual :)
appOnProduction
.fork( e => console.error(e)
, x => console.log(x)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment