Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var async = require('async');
function getUserFriends(userName, next) {
db.users.findOne({name:userName}, foundUser);
var glob = {};
function foundUser(err, user) {
if (err != null) return next(err);
glob.user = user;
getFriendsById(user.id, gotFriends);
}
function gotFriends(err, friends) {
if (err != null) return next(err);
glob.friends = friends;
if (glob.user.type == 'power user') {
async.map(friends, getFriendsById, wtfFriendAction);
} else {
return next(null, friends);
}
}
function wtfFriendAction(err, friendsOfFriends) {
for (var i = 0; i < friendsOfFriends.length; i++) {
for (var j = 0; j < friendsOfFriends[i].length; j++) {
if (glob.friends.indexOf(friendsOfFriends[i][j]) != -1) {
glob.friends.push(friendsOfFriends[i][j]);
}
}
}
return next(null, glob.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');
function getUserFriends(userName, next) {
db.users.findOne({name:userName}, foundUser);
var user, friends;
function foundUser(err, _user) {
if (err != null) return next(err);
user = _user;
getFriendsById(user.id, gotFriends);
}
function gotFriends(err, _friends) {
if (err != null) return next(err);
friends = _friends;
if (user.type == 'power user') {
async.map(friends, getFriendsById, wtfFriendAction);
} else {
return next(null, friends);
}
}
function wtfFriendAction(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);
}
}
function getFriendsById(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
You can’t perform that action at this time.