Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.