I am trying to run a script that does the same thing multiple times, but I don't want to get stuck writing callback after callback. I am a bit of a beginner with node.js, so I am not sure the proper way of doing this. Here is my code below:
exports.index = function (req,res) {
/* some code */
something.execute(this, that, function(results) {
for (var i = 2; i <= 10; i++) {
var nextPage = getNextPage(i);
}
}
}
function getNextPage(page) {
something.execute(this, that, function(results) {
return results;
}
}
What happens is that result returns as undefined. I know that this has to do something to do with that getNextPage is completed after the rest of the code because node is asynchronous, but I am unsure what the best way to approach this is.
Any suggestions? Thanks!
Edit: Added code in getNextPage. I am pretty much running the same code for 9 more pages. It seems as though I need to figure out how to force the something.execute loop to run.