Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Looping through x after all i : node.js

Looping through x after all i : node.js

Problem

I have this code. I want to Loop through all Users in a database and for each user get a list of their Portfolios (which in turn is a collection of stocks):

 calculatePortfolios: function(callback) {
    var thisuser;
    module.exports.getAllUsers(function(err, users) {
      /* Loop through all users with i */
      for(var i = 0; i 

The result I get is that all i indices (users) are looped through before all x indices (portfolios). Shouldn't x be an inner loop of i?

Is this something related to how Node.JS works?

Any help is much appreciated. Thank you.

Problem courtesy of: Sam Saint-Pettersen

Solution

@PSL: Got it working with async.waterfall. Probably not the most elegant solution, but it works for now.

 /**
  * @description Calculate the value of an investor's portfolio.
  * @function
 */
 calculatePortfolios: function(callback) {
     async.waterfall([
       function(callback) {
         var allUsers = [];
         module.exports.getAllUsers(function(err, users) {
           for(var i = 0; i 
Solution courtesy of: Sam Saint-Pettersen

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Looping through x after all i : node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×