Reputation: 1984
I have a function with callback, where I'm using "listTables" method of dynamoDB, which returns just 100 table names and if there is anymore tables, it returns another field called "LastEvaluatedTableName" which we can use in our new query in listTables to query another 100 tables from the mentioned "LastEvaluatedTableName"; how can I have recursion in callbacks in javascript in this logic?
I have tried the following which is not correct:
module.exports.ListTables = function (start, callback) {
var params;
if (start) {
params = {
"ExclusiveStartTableName": start
};
}
dynamodb.listTables(params, function (err, data) {
var totalData = [];
totalData.push(data);
if (data.LastEvaluatedTableName) {
data = module.exports.ListTables(data.LastEvaluatedTableName);
}
callback(err, totalData);
});
}
Please let me know if you need more clarifications!
Thanks!
Upvotes: 0
Views: 122
Reputation: 37701
You need to concat your data, not replace it each time:
dynamodb.listTables(params, function (err, data) {
if (data.LastEvaluatedTableName) {
data.concat(module.exports.ListTables(data.LastEvaluatedTableName));
}
callback(err, data);
});
UPDATE
Based on the info from the comment, sounds like you need something like this:
module.exports.ListTables = function (start, callback, totalData) {
var params;
if (start) {
params = {
"ExclusiveStartTableName": start
};
}
if (!totalData) {
totalData = [];
}
dynamodb.listTables(params, function (err, data) {
totalData = totalData.concat(data.TableNames);
if (data.LastEvaluatedTableName) {
module.exports.ListTables(data.LastEvaluatedTableName, callback, totalData);
}
else {
callback(err, totalData);
}
});
}
Upvotes: 1