title: Coffescript Nested Do Loop / Async Nested Loop
body: <p>I want to save into two collections in my mongoDB. This operations are async so I use for and do in coffee.</p>
  
  <pre><code>for machine in machines
    do(machine) -&gt;
      //if machine does not exist
        for part in machine.parts
          do(part) -&gt;
            //if not part not exists --&gt; save
            //push part to machine parts list
        //save machine
  </code></pre>
  
  <p>The machine parts are empty later in the db. How can I make the first do loop wait for the second do loop to finish?</p>
  
  <p>EDIT Real Code Example:</p>
  
  <pre><code>  recipeModel = require('../models/recipe.model')
    ingredientModel = require('../models/ingredient.model')
  
    #Save Recipe into the database
    async.map recipes, (recipe, next) -&gt;
      recipeDBObject = new recipeModel()
      recipeDBObject.href = recipe.href
      recipeDBObject.ingredients = []
      recipeModel.find({ href: recipe.href }, (err, recipeFound) -&gt;
        return next err if err
        return next null, recipeFound if recipeFound.length &gt; 0
  
        recipeDBObject.title = recipe.title
        ingredientsPushArray = []
        console.log recipe.href
  
        async.map recipe.zutaten, (ingredient, cb) -&gt;
          #Save all ingredients
          ingredient.idName = ingredient.name.replace(/[^a-zA-Z0-9]+/gi, "").toLowerCase()
          ingredientModel.find({ idName: ingredient.idName }, (err, ingredientFound) -&gt;
            return next err if err
            if ingredientFound.length &gt;0
              ingredientDBObject = ingredientFound[0]
            else
              ingredientDBObject = new ingredientModel()
  
            ingredientDBObject.name = ingredient.name
            ingredientDBObject.save()
  
            recipeDBObject.ingredients.push({"idName":ingredient.idName, "name":ingredient.name, "amount":ingredient.amount})
            return cb(null, true)
          )
        recipeDBObject.ingredients = ingredientsPushArray
        recipeDBObject.save()
        return next(null, true)
      )
  </code></pre>
  
  <p>I still don't get it working. Recipes are saved, node builds the ingredients array but it neither saves the ingredients nor does it save the array into the recipes.</p>
  
  <p>EDIT 2:</p>
  
  <pre><code>  async.map recipes,
      (recipe, next) -&gt;
        recipeDBObject = new recipeModel()
        recipeDBObject.href = recipe.href
        recipeDBObject.ingredients = []
        recipeModel.find({ href: recipe.href }, (err, recipeFound) -&gt;
          return next err if err
          return next null, recipeFound if recipeFound.length &gt; 0
  
          recipeDBObject.title = recipe.title
          ingredientsPushArray = []
  
          ingredientsArray = []
          async.map recipe.zutaten,
            (ingredient, cb) -&gt;
              #Save all ingredients
              ingredient.idName = ingredient.name.replace(/[^a-zA-Z0-9]+/gi, "").toLowerCase()
              ingredientModel.find({ idName: ingredient.idName }, (err, ingredientFound) -&gt;
                return next err if err
                ingredientsArray.push({"idName":ingredient.idName, "name":ingredient.name, "amount":ingredient.amount})
                if ingredientFound.length &gt;0
                  return cb(null, true)
                else
                  ingredientDBObject = new ingredientModel()
                  ingredientDBObject.name = ingredient.name
                  ingredientDBObject.idName = ingredient.idName
                  ingredientDBObject.save((err) -&gt;
                    #console.log "some erros because required is empty" if err
                    return cb err if err
                    #console.log "ingredient saved"
                    return cb(null, true)
                  )
            (err, ingredientsArray) -&gt;
              console.log "This is never logged"
              return err if err
              recipeDBObject.ingredients = ingredientsArray
              recipeDBObject.save((err)-&gt;
                return err if err
                return next(null, true)
            )
          )
        )
      (err) -&gt;
        console.log "show me the errors: ", err if err
  </code></pre>
  
  <p>Now the ingredients are saved but the recipes aren't.</p>
  
  <p>Interesting ressources:
  <a href="http://www.hacksparrow.com/managing-nested-asynchronous-callbacks-in-node-js.html" rel="nofollow">http://www.hacksparrow.com/managing-nested-asynchronous-callbacks-in-node-js.html</a></p>