title: Angular Promises Sequential loop
body: <p>I have the following set of data.</p>
  
  <pre><code> $scope.orders = [
          { material: 'A', quantity: 32, orderNumber: 'dummy'},
          { material: 'A', quantity: 65, orderNumber: 'dummy'},
          { material: 'A', quantity: 86, orderNumber: 'dummy'},
  
          { material: 'B', quantity: 45, orderNumber: 'dummy'},
          { material: 'B', quantity: 68, orderNumber: 'dummy'},
          { material: 'B', quantity: 15, orderNumber: 'dummy'},
  
          { material: 'C', quantity: 11, orderNumber: 'dummy'}
      ];
  </code></pre>
  
  <p>I want to process (createOrder) the orders grouped by material. After all the orders for that particular material have been processed I want to call another function (materialRun) that does a material run. After this function is successful the next material (with its corresponding orders) has to be processed etc.. All these calls have to be sequential because the backend cannot process everything in parallel. </p>
  
  <p>So i'm searching for something like this:</p>
  
  <p>material A: order 1 -> order 2 -> order 3 -> materialRun</p>
  
  <p>when the materialRun from A is done, start material B</p>
  
  <p>material B: order 1 -> order 2 -> order 3 -> materialRun</p>
  
  <p>when the materialRun from B is done, start material C</p>
  
  <p>...</p>
  
  <p>I also need the result from each createOrder to update the orders list</p>
  
  <p>I'm using angular promises for now but i'm open for suggestions.
  I hope this fiddle helps: <a href="http://jsfiddle.net/a1sp0ye2/" rel="nofollow">http://jsfiddle.net/a1sp0ye2/</a></p>