Skip to content

Instantly share code, notes, and snippets.

@tim-smart
Created August 21, 2013 04:32
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 tim-smart/6290338 to your computer and use it in GitHub Desktop.
Save tim-smart/6290338 to your computer and use it in GitHub Desktop.
var async = require('async');
function getUserFriends(userName, next) {
var s = {}
db.users.findOne({name:userName}, foundUser);
function foundUser(err, user) {
if (err != null) return next(err);
s.user = user
getFriendsById(s.user.id, gotFriends);
}
function gotFriends(err, friends) {
if (err != null) return next(err);
s.friends = friends
if (s.user.type == 'power user') {
async.map(s.friends, getFriendsById, wtfFriendAction);
} else {
return next(null, s.friends);
}
}
function wtfFriendAction(err, friendsOfFriends) {
for (var i = 0; i < friendsOfFriends.length; i++) {
for (var j = 0; j < friendsOfFriends[i].length; j++) {
if (s.friends.indexOf(friendsOfFriends[i][j]) != -1) {
s.friends.push(friendsOfFriends[i][j]);
}
}
}
return next(null, s.friends);
}
}
function getFriendsById(userId, next) {
db.friends.find({userId:userId}, function (err, friends) {
if (err != null) return next(err);
return next(null, friends);
});
}
var async = require('async');
var getUserFriends = function (userName, next) {
db.users.findOne({name:userName}, function (err, user) {
if (err != null) return next(err);
getFriendsById(user.id, function (err, friends) {
if (err != null) return next(err);
if (user.type == 'power user') {
async.map(friends, getFriendsById, function (err, friendsOfFriends) {
for (var i = 0; i < friendsOfFriends.length; i++) {
for (var j = 0; j < friendsOfFriends[i].length; j++) {
if (friends.indexOf(friendsOfFriends[i][j]) != -1) {
friends.push(friendsOfFriends[i][j]);
}
}
}
return next(null, friends);
});
} else {
return next(null, friends);
}
});
});
}
var getFriendsById = function (userId, next) {
db.friends.find({userId:userId}, function (err, friends) {
if (err != null) return next(err);
return next(null, friends);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment