How to use the fluture.fold function in fluture

To help you get started, we’ve selected a few fluture examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github StuartHarris / redux-future-example / src / App / actions.js View on Github external
export const getSchedule = fetchJson => {
  // makeUrl :: a -> a
  const makeUrl = day => `http://www.bbc.co.uk/radio4/programmes/schedules/fm/${day}.json`;
  // httpGet :: a -> Future RemoteData e a
  const httpGet = compose(fold(Failure, Success), futurize(fetchJson));
  // fetchData :: a -> Future RemoteData e a
  const fetchData = compose(httpGet, makeUrl);
  // getBroadcasts :: a -> Array b
  const getBroadcasts = path(['schedule', 'day', 'broadcasts']);
  // Broadcast :: a -> a -> a -> b
  const Broadcast = tagged('id', 'title', 'start');
  // toBroadcast :: a -> b
  const toBroadcast = a => Broadcast(a.pid, path(['programme', 'display_titles', 'title'], a), a.start);
  // transform :: RemoteData e a -> RemoteData e b
  const transform = a => {
    if (!is(Success, a)) {
      return a;
    }
    return Success(map(toBroadcast)(getBroadcasts(a.data)));
  };
github jongold / unblockerer / index.js View on Github external
const stabilizeFutures = xs => xs.map(Future.fold(Either.Left, Either.Right));
github fluture-js / momi / examples / bootstrap / app.js View on Github external
'use strict';

const {App, Middleware} = require('../../');
const {prop, assoc} = require('ramda');
const {getService, putService} = require('./util');
const createError = require('http-errors');
const qs = require('querystring');
const Future = require('fluture');

const REQ = Middleware.get.map(prop('req'));
const QUERY = Middleware.get.map(prop('query'));
const error = (code, message) => Middleware.lift(Future.reject(createError(code, message)));

const No = x => ({ok: false, value: x});
const Yes = x => ({ok: true, value: x});
const attempt = Middleware.hoist(Future.fold(No, Yes));

const errorToResponse = e => ({
  status: e.status || 500,
  headers: {},
  body: JSON.stringify({message: e.message, name: e.name})
});

//Export an app.
module.exports = App.empty()

//Error handling middleware.
.use(App.do(function*(next) {
  const res = yield attempt(next);
  return res.ok ? res.value : errorToResponse(res.value);
}))