Skip to content

Instantly share code, notes, and snippets.

@shinecita
Created October 15, 2012 15:17
Show Gist options
  • Save shinecita/3893047 to your computer and use it in GitHub Desktop.
Save shinecita/3893047 to your computer and use it in GitHub Desktop.
forEachSeries
function checkUserTags(user) {
var tags = user.tags
var newTags = [];
console.log( "Checking user", user._id)
if (tags.length > 0) {
tags.forEach(function (tagName, i) {
console.log("user", user._id, tagName, i);
Tag
.where('name', tagName)
.findOne(function(err, tag) {
if (!tag) {
var newtag = { name : tagName, type : 'award'}
Tag.create(newtag, function(err, newtag) {
console.log(user);
if (err) {
console.log("error", err)
doneCheckingTags(i)
} else {
console.log("tag created", newtag.name)
newTags.push(newtag._id);
doneCheckingTags(i)
}
});
} else {
doneCheckingTags(i);
newTags.push(tag._id);
}
});
});
} else {
console.log("User has no tags");
}
function doneCheckingTags(i) {
if (i+1 >= tags.length) {
updateUserTag(user._id, newTags);
}
}
}
OldUser
.find(function (err, users) {
async.forEachSeries(users, checkUserTags, function (err) {
console.log("done", err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment