gpt4 book ai didi

node.js - 如何在 async.each 循环中跳出异步系列 - node.js

转载 作者:搜寻专家 更新时间:2023-10-31 23:18:29 28 4
gpt4 key购买 nike

我需要跳过函数或 async.series 的突破,并且想知道我应该如何去做。我有一系列需要迭代的项目。我将该列表放在 async.each 函数中。数组中的每个项目然后在继续之前通过一系列所需的功能列表(因为下一个需要来自一个的信息)。但在某些情况下,我只需要遍历第一个函数,然后如果不满足条件(例如,它是我们不使用的类别),则回调到下一个项目的 async.each 循环。这是我的代码示例:

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
});
}

最佳答案

将退出快捷方式放在依赖函数的顶部。例如:

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

关于node.js - 如何在 async.each 循环中跳出异步系列 - node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17796924/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com