Skip to content

Instantly share code, notes, and snippets.

@radiosilence
Created August 17, 2013 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radiosilence/2fd82aaf3b1721448ae1 to your computer and use it in GitHub Desktop.
Save radiosilence/2fd82aaf3b1721448ae1 to your computer and use it in GitHub Desktop.
async = require "async"
getUserFriends = (userName, next) ->
handleUser = (err, user) ->
return next(err) if err?
getFriendsById user.id, doAsync
doAsync = (err, friends) ->
return next(err) if err?
if user.type is "power user"
async.map friends, getFriendsById, getFriendsOfFriends
else
next null, friends
getFriendsOfFriends = (err, friendsOfFriends) ->
i = 0
while i < friendsOfFriends.length
j = 0
while j < friendsOfFriends[i].length
friends.push friendsOfFriends[i][j] unless friends.indexOf(friendsOfFriends[i][j]) is -1
j++
i++
next null, friends
getFriendsById = (userId, next) ->
db.friends.find
userId: userId
, (err, friends) ->
return next(err) if err?
next null, friends
db.users.findOne
name: userName
, handleUser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment