Skip to content

Instantly share code, notes, and snippets.

@jkrems
Created December 4, 2013 22:03
Show Gist options
  • Save jkrems/7796436 to your computer and use it in GitHub Desktop.
Save jkrems/7796436 to your computer and use it in GitHub Desktop.
Clarity of intend
function getUser(id) {
// implementation unknown
}
function getUserName(id) {
// is getUser(id) returning Promise[String]? String? - both work fine
return when(when(id, getUser), function(user) {
return user.name;
});
}
function findLongest(values) {
// returns value with max value.length
}
var mixed = [ // user ids from different sources
'foo',
Promise.resolve('bar'),
10
];
// we don't have to care about whether it's a value or a
// promise of a value - conceptually (in the problem domain)
// it's some user's id. the fact that the when-call above is
// "automagically" unifying the types lets us focus on expressing
// the actual problem instead of talking about non-domain
// abstractions
var names = mixed.map(getUserName); // Array[Promise[String]
all(names, findLongest).then(function(longestName) {
console.log(longestName);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment