【发布时间】:2017-06-23 12:19:43
【问题描述】:
我无法让嵌套的async.each 工作,我不确定为什么会收到错误:Error: Callback was already called,即使我已正确放置回调。
checkDefaultOverlap: function(default_shifts, done) {
async.each(default_shifts, function(default_shift, next) {
var subarray = default_shifts.slice(default_shifts.indexOf(default_shift) + 1, default_shifts.length - 1);
async.each(subarray, function(default_shift2, next) {
default_shift.week_days.map(function(day1) {
default_shift2.week_days.map(function(day2) {
if (day1 === day2 &&
default_shift.start <= default_shift2.end && default_shift2.start <= default_shift.end)
next({error: 'The shifts overlap!'});
});
});
next();
}, function(err) {
if (err) next(err);
else next(null);
});
}, function(err) {
if (err) return done(err);
else return done(null);
});
}
}
任何帮助将不胜感激。
【问题讨论】:
-
哇,你是回调地狱,请重构你的代码,让它更易读
-
作为第一步,您可以删除内部
async.each()并将其替换为循环或循环的声明性版本。
标签: javascript node.js each async.js