Skip to content

Instantly share code, notes, and snippets.

@mvaldesdeleon
Created August 8, 2017 13:52
Show Gist options
  • Save mvaldesdeleon/fbdae29e1ef17f976a3bbbae8358cad5 to your computer and use it in GitHub Desktop.
Save mvaldesdeleon/fbdae29e1ef17f976a3bbbae8358cad5 to your computer and use it in GitHub Desktop.
Quilombo Driven Development
const wait = n => new Promise(res => setTimeout(res, n));
const log = console.log.bind(console);
const dummy = payload => wait(1000).then(() => payload);
const user = () => (log('user'), dummy({user: 'john'}));
const tweets = () => (log('tweets'), dummy({tweets: ['asdasd', 'dsada']}));
const github = () => (log('github'), dummy({repos: ['asdasd', 'dsada']}));
// buildResult :: (user, tweets, github) -> {user, tweets, github}
const buildResult = (user, tweets, github) => ({user, tweets, github});
// Promise.all :: [Promise(*)] -> Promise([*])
function fetchResult() {
return Promise.all([user(), tweets(), github()])
.then(([user, tweets, github]) => buildResult(user, tweets, github));
}
// buildResult' :: (Promise(user), Promise(tweets), Promise(github)) -> Promise({user, tweets, github})
function buildResult2(userP, tweetsP, githubP) {
return Promise.all([userP, tweetsP, githubP])
.then(([user, tweets, github]) => buildResult(user, tweets, github));
}
function buildResult3(...args) {
return Promise.all(args)
.then(args => buildResult(...args));
}
function fetchResult2() {
return buildResult2(user(), tweets(), github());
}
function fetchResult3() {
return buildResult3(user(), tweets(), github());
}
//
const liftP = fn => (...args) => Promise.all(args).then(args => fn(...args));
function fetchResultsLift() {
return liftP(buildResult)(user(), tweets(), github());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment