I need to skip functions or breakout of a an async.series, and was wondering how I should go about it. I have an array of items that I need to iterate through. I put that list in a async.each function. Each item in the array then goes through a list of functions needed before moving on, in a series (as info from one is needed in the next). But in some cases, I only need to go through the first function, then if a condition isn't met (example, it is a category we don't use), then callback to the async.each loop for the next item. Here is an example of my code:
exports.process_items = function(req, res, next){
var user = res.locals.user;
var system = res.locals.system;
var likes = JSON.parse(res.locals.likes);
var thecat;
//process each item
async.each(items, function(item, callback){
//with each item, run a series of functions on it...
thecat = item.category;
async.series([
//Get the category based on the ID from the DB...
function(callback) {
//do stuff
callback();
},
//before running other functions, is it an approved category?
//if it is not an approved category, SKIP THE OTHER FUNCTIONS IN THE LIST (but how?)
function(callback) {
//do stuff
callback();
},
//some other functionality run on that item,
function(callback){
//do stuff
callback():
}
], function(err) {
if (err) return next(err);
console.log("done with series of functions, next item in the list please");
});
//for each like callback...
callback();
}, function(err){
//no errors
});
}
Put exit shortcuts at the top of the dependent functions. For example:
async.series([
//Get the category based on the ID from the DB...
function(callback) {
//do stuff
callback();
},
//before running other functions, is it an approved category?
//if it is not an approved category, SKIP THE OTHER FUNCTIONS IN THE LIST (but how?)
function(callback, results) {
if (results[0] is not an approved category) return callback();
//do stuff
callback();
},