我不确定我是否理解了你想要实现的目标,但我会尝试。
第一个问题是,在内部回调函数中,您无法为外部函数返回值(有关此信息,请参见https://dev59.com/PGw15IYBdhLWcg3wFHpZ#6847754)。
此外,您正在返回回调函数而不是结果。
这是我认为您实际想要做的:
var async = require("async");
var listFunctions = {
test1: function(objectData, callbackData) {
objectData.name += ":a";
callbackData(null, objectData);
},
test2: function(objectData, callbackData) {
objectData.name += ":b";
objectData.foo = "bar";
callbackData(null, objectData);
},
test3: function(objectData, callbackData) {
objectData.name += ":c";
callbackData(null, objectData);
},
test4: function(objectData, callbackData) {
objectData.name += ":d";
callbackData(null, objectData);
},
test5: function(objectData, callbackData) {
objectData.name += ":e";
callbackData(null, objectData);
}
};
function abcd(objectData, cb) {
listFunctions.test1(objectData, function(err, data1) {
listFunctions.test2(data1, function(err, data2) {
listFunctions.test3(data2, function(err, data3) {
listFunctions.test4(data3, function(err, data4) {
listFunctions.test5(data4, function(err, data5) {
cb(err, data5);
});
});
});
});
});
}
function initalFunction(objectData, cb) {
var results = [];
async.each(objectData, function(dataValues, done) {
abcd(dataValues, function(err, response) {
results.push(response);
done();
});
}, function(err) {
cb(err, results);
});
}
var objectData = [{id:1,name:"abcd"},{id:2,name:"abc2d"},{id:3,name:"3abcd"},{id:4,name:"4abcd"}];
initalFunction(objectData, function(err, response) {
if (err) {
console.log(err.stack);
return;
}
console.log("Result:", response);
});
现在让我们使用
async.seq
和
async.map
来简化这个过程:
var async = require("async");
var listFunctions = {
test1: function(objectData, callbackData) {
objectData.name += ":a";
callbackData(null, objectData);
},
test2: function(objectData, callbackData) {
objectData.name += ":b";
objectData.foo = "bar";
callbackData(null, objectData);
},
test3: function(objectData, callbackData) {
objectData.name += ":c";
callbackData(null, objectData);
},
test4: function(objectData, callbackData) {
objectData.name += ":d";
callbackData(null, objectData);
},
test5: function(objectData, callbackData) {
objectData.name += ":e";
callbackData(null, objectData);
}
};
var abcd=async.seq.apply(null, [
listFunctions.test1,
listFunctions.test2,
listFunctions.test3,
listFunctions.test4,
listFunctions.test5
]);
function initialFunction(objectData, cb) {
async.map(objectData, abcd, cb);
}
var objectData = [{id:1,name:"abcd"},{id:2,name:"abc2d"},{id:3,name:"3abcd"},{id:4,name:"4abcd"}];
initialFunction(objectData, function(err, response) {
if (err) {
console.log(err.stack);
return;
}
console.log("Result:", response);
});
如果你能将
listFunctions
从对象改为数组:
var async = require("async");
var listFunctions = [
function test1(objectData, callbackData) {
objectData.name += ":a";
callbackData(null, objectData);
},
function test2(objectData, callbackData) {
objectData.name += ":b";
objectData.foo = "bar";
callbackData(null, objectData);
},
function test3(objectData, callbackData) {
objectData.name += ":c";
callbackData(null, objectData);
},
function test4(objectData, callbackData) {
objectData.name += ":d";
callbackData(null, objectData);
},
function test5(objectData, callbackData) {
objectData.name += ":e";
callbackData(null, objectData);
}
];
function initialFunction(objectData, cb) {
async.map(objectData, async.seq.apply(null, listFunctions), cb);
}
var objectData = [{id:1,name:"abcd"},{id:2,name:"abc2d"},{id:3,name:"3abcd"},{id:4,name:"4abcd"}];
initialFunction(objectData, function(err, response) {
if (err) {
return console.error(err.stack);
}
console.log("Result:", response);
});
如果不过仍需要处理动态listFunctions
:
function initialFunction(objectData, cb) {
var list = Object.keys(listFunctions).map(function(name) {
return listFunctions[name];
});
async.map(objectData, async.seq.apply(null, list), cb);
}
希望这能有所帮助。