Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const Future = require('fluture');
const R = require('ramda');
const {Reader} = require('monet');
const FakeDb = () => {
const data = {
1: {id: 1, name: 'bob', bff: 2},
2: {id: 2, name: 'alice', bff: 1}
};
return { byId: id => Promise.resolve(data[id]) };
};
const upperName = data => data.name.toUpperCase();
const getUser1 = Reader(db => db.byId(1));
const getBestFriend = data => Reader(db => db.byId(data.bff));
const ReaderPromise = value => {
const map = fn => ReaderPromise(
value.map(
promise => promise.then(fn)
)
);
const chain = fn => ReaderPromise(
value.chain(
promise => Reader(
config => promise.then(
(...args) => fn(...args).run(config)
)
)
)
);
return Object.freeze({chain, map, run: (config) => value.run(config)});
}
const program = ReaderPromise(getUser1)
.chain(data => ReaderPromise(getBestFriend(data)))
.map(upperName)
program.run(FakeDb()).then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.