Created
October 15, 2012 15:17
-
-
Save shinecita/3893047 to your computer and use it in GitHub Desktop.
forEachSeries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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