-
-
Save radiosilence/2fd82aaf3b1721448ae1 to your computer and use it in GitHub Desktop.
This file contains 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
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