I have the following set of data.
$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'}
];
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.
So i'm searching for something like this:
material A: order 1 -> order 2 -> order 3 -> materialRun
when the materialRun from A is done, start material B
material B: order 1 -> order 2 -> order 3 -> materialRun
when the materialRun from B is done, start material C
...
I also need the result from each createOrder to update the orders list
I'm using angular promises for now but i'm open for suggestions. I hope this fiddle helps: http://jsfiddle.net/a1sp0ye2/