How to exit async series inside async.each loop - node.js

I need to skip the functions or aync.series breakthrough, and I was wondering how I should do this. I have an array of elements that I need to iterate over. I put this list in the async.each function. Each element of the array then iterates over the list of functions necessary before moving on in the series (since information from one of them is required in the next). But in some cases, I only need to go through the first function, then if the condition is not met (for example, this is a category that we do not use), and then the callback in the async.each loop for the next element. 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 }); } 
+4
source share
1 answer

Set shortcuts at the top of the dependent functions. For instance:

 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(); }, 
+3
source

Source: https://habr.com/ru/post/1492787/


All Articles