Created
December 4, 2013 22:03
-
-
Save jkrems/7796436 to your computer and use it in GitHub Desktop.
Clarity of intend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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