我在节点模块中有一个函数,它是一组三个嵌套回调,它工作得很好,给了我需要的线性,因为每个嵌套回调都依赖于前一个回调的数据。问题是第二个函数的回调需要冒泡并递归调用其父函数。它与外部 API 进行通信。这是实际代码,变量重命名以混淆我的超级顶级 sekrit 业务逻辑:
exports.account_usage = function (req, res) {
var domainID = req.body.domains,
startDate = req.body.date_start,
endDate = req.body.date_end,
accountItems = {},
usage = {},
domainStats = {},
page = 0;
//req.cs is a module that communicates with an external API to gather usage data
req.cs.exec("listAccountItems", {
"listall": "true",
"domainid": domainID
},
function (error, result) {
accountItems = result.item;
console.log("listAccountItems callback");
//Get Usage Records
req.cs.exec("listUsageRecords", {
"startdate": startDate,
"enddate": endDate,
"domainid": domainID,
"page": page,
"pagesize": 2000 //try not to DDOS the server. only fetch 2000 records at a time
}, function (error, result) {
usage = req._.extend(usage, result.usagerecord); //this is underscore
console.log("Usage Records: " + usage.length);
page++;
//right here, if result.length === 2000, I need to call
// listUsageRecords until result.length < 2000
//got list of Usage,
//now process usage here
//filter usage item 1
var bytesrecords1 = req._.filter(usage, function (usage) {
return (usage.usagetype === 4);
});
//sum
var bytes1 = req._.reduce(bytesrecords1, function (memo, record) {
return memo + parseInt(record.rawusage, 10);
}, 0);
console.log("Bytes1: " + bytes1);
domainStats.bytes1 = (((bytes1 / 1000) / 1000) / 1000).toFixed(4);
//filter usage item 2
var bytesrecords2 = req._.filter(usage, function (usage) {
return (usage.usagetype === 5);
});
//sum
var bytes2 = req._.reduce(bytesrecords2, function (memo, record) {
return memo + parseInt(record.rawusage, 10);
}, 0);
console.log("Bytes2: " + bytes2);
domainStats.bytes2 = (((bytes2 / 1000) / 1000) / 1000).toFixed(4);
req._.each(accountItems, function (account) {
//get runnning hours
var recs = req._.filter(usage, function (usage) {
return (usage.accountid === account.id && usage.usagetype === 1);
});
account.usage = req._.reduce(recs, function (memo, record) {
return memo + parseInt(record.rawusage, 10);
}, 0);
//filter all the recs for each usage type, 1-14
console.log("Account Usage: " + account.usage);
console.log("Account ID: " + account.name);
});
console.log("ready to render");
res.render('usage', {
"title": "Usage Report",
"domain": domainStats
});
});
});
};
实际代码也在这个小提琴中:[http://jsfiddle.net/3wTQA/1/][1] 我一直使用谷歌,直到我的手指流血,我不知道如何阻止内部回调继续的递归。API 需要来自外部 API 的分页,以防止在需要获取大型数据集的情况下远程系统上的 DDOS。
编辑:这是实际代码,经过注释和清理,并附有一些我试图提取的数据示例:http: //jsfiddle.net/3wTQA/1/