Skip to content

Instantly share code, notes, and snippets.

@snewell92
Created October 18, 2017 20:46
Show Gist options
  • Save snewell92/ff7a0b2482293cbd785879fe80f4587a to your computer and use it in GitHub Desktop.
Save snewell92/ff7a0b2482293cbd785879fe80f4587a to your computer and use it in GitHub Desktop.
Context
import { curry, flowRight as compose } from 'lodash';
const Task = require('folktale/concurrency/task');
/* example code, pieces from application */
/* Imagine a functional-progrmaming style Forgot Password workflow */
usernameOrEmailExists = (input: {/*...*/}) => isValid(input)
? Task.of(input.username)
: Task.rejected('Invalid');
// compChain and compMap take whatever and call map or chain on them (a la folktale map/chain on Task or Result, or whatever)
// excutes the task and returns a promise - so Feathers (or express, or microservice framework, or whatever)
// can handle it appropriately
const runAndReturn = (t: Task) => t.run().promise();
// compose from right to left (or in this case, bottom up)
let processRequest = compose(
runAndReturn,
compChain(saveUserToken(persistUser, getExpiration)),
compChain(getUsernameAndToken(getUserData, getKey)),
compMap(buildFindUserQuery),
usernameOrEmailExists);
// alternatively, I can declare my own function which does this:
let app = (input) =>
usernameOrEmailExists(input)
.map(buildFindUserQuery)
.chain(getUsernameAndToken(getUserData, getKey))
.chain(saveUserToken(persistUser, getExpiration));
// then pass this as service handler, request handler, or whatever larger context we're in
let processedReq = compose(runAndReturn, app);
// expose processRequest as a service call, or whatever. Express route, Feathers service, microservice call, whatever
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment