2 ms·
Promises + CoffeeScript really hits the sweet spot for me: getUserFriends = (userName) -> db.users.findOne(name: userName).then (user) -> db.
by tlrobinson 13y ago
Promises + CoffeeScript really hits the sweet spot for me:
getUserFriends = (userName) ->
db.users.findOne(name: userName).then (user) ->
db.friends.find userId: user.id
Or prezjordan's example:
getUserFriends = (userName) ->
db.users.findOne(name: userName)
.then (user) ->
db.friends.find userId: user.id
.then(function (friends) ->
somethingAsync(friends)
The concise function syntax and implicit "return" really helps.
If that's not good enough you can implement higher level control flow abstractions much more nicely than with raw callbacks, e.x. https://github.com/tlrobinson/q-step https://github.com/tlrobinson/q-step
getUserFriends = (userName) ->
QStep(
->
db.users.findOne name: userName
(user) ->
db.friends.find userId: user.id
(friends) ->
somethingAsync friends
)