title: NodeJS+Mongoose - Need help Refactor code using Promises
body: <p>Consider this example :</p>
  
  <pre><code>router.post('/', function (req, res, next) {
  
  var order = new Order({
  
      customer_id: req.body.customer_id,
      order_elements: []
  });
  
  order.save(function(err, saved_order) {
  
      var elementsSaveCount = 0;
  
      req.body.order_elements.forEach( function(order_element, index) {
  
          orderElement = new OrderElement({
  
              order_id: saved_order._id,
              name: order_element.name
          });
  
          orderElement.save(function(err, result) {
  
              elementsSaveCount++;
              order.order_elements.push(result._id);
  
              if(elementsSaveCount &gt;= req.body.order_elements.length) {
  
                  saved_order.save();
                  res.status(201).json({
                  message: 'order saved',
                  order: order
                  });
              }
          });
      });
  });
  });
  </code></pre>
  
  <p>The 'Order' model has an array of id of 'OrderElements'.
  First I'm saving the Order, then inside the callback of the save() I loop through the OrderElements of the request to save them as well and push each id inside the Order.
  I use the variable 'elementsSaveCount' to check when all the save() are executed.</p>
  
  <p>I'd like to refactor this code using promises.</p>