Skip to content

Instantly share code, notes, and snippets.

@mikekunze
Created August 17, 2011 15:28
Show Gist options
  • Save mikekunze/1151779 to your computer and use it in GitHub Desktop.
Save mikekunze/1151779 to your computer and use it in GitHub Desktop.
Multi-deep async
var p_roles = mods.mongoose.model('p_roles');
var accounts = mods.mongoose.model('accounts');
var ninAccounts = [];
var inAccounts = [];
p_roles.find({ role_id: query.role_id }, function(err, docs) {
var async = mods.async;
var pushNinAccounts = function(item, callback) {
if(item) {
accounts.findOne({ _id: item.account_id }, function(err, account) {
if(account != null) {
ninAccounts.push(account._id);
callback();
} else callback();
});
}
};
async.forEach(docs, pushNinAccounts , function(err) {
if(err)
console.log(err);
accounts.find({ _id: { $nin: ninAccounts } }, function(err, docs) {
var pushinAccounts = function(item, callback) {
if(item) {
var pushItem = {
description: item.name,
_id: item._id,
role_id: query.role_id
};
inAccounts.push(pushItem);
callback();
} else callback();
};
async.forEach(docs, pushinAccounts, function(err) {
if(err)
console.log(err);
var response = {
success: true,
results: inAccounts.length,
items: inAccounts
}
res.send(response);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment