3

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/

6
  • 1
    add in post code what you try, not only link to fiddle. Commented Apr 22, 2015 at 12:19
  • So what output you expected? Commented Apr 22, 2015 at 12:21
  • 1
    "All these calls have to be sequential because the backend cannot process everything in parallel." - uh, you know, maybe you should fix the backend? It would be reasonable at least if you could put all updates in a single request, and let the backend figure out how to process that. Commented Apr 22, 2015 at 12:38
  • @Grundy the table updated with the correct order numbers, I will also add some more feedback when an order has been succesfully processed. Like a background color on the table row or something like that Commented Apr 22, 2015 at 12:41
  • @Stvenoo correct order numbers how you detect correct numbers? Commented Apr 22, 2015 at 12:44

2 Answers 2

2

The following is a template for loops with asynchronous bodies, i.e. the next iteration must take place only when an asynchronous task is complete. It works for promises in an Angular context, but can be generalized for any promises implementation I believe:

/**
 * Loop the items array asynchronously.
 *
 * @param items - Array to iterate
 * @param doLoopBody - Callback that executes the body loop, returns promise
 */
function asyncLoop(items, doLoopBody) {
    var i = 0, d = $q.defer();

    nextIteration();

    return d.promise;

    function nextIteration() {
        if( i < items.length ) {
            doLoopBody(items[i], i, items).then(
                function() {
                    i++;
                    nextIteration();
                },
                onError
            );
        }
        else {
            d.resolve();
        }
    }

    function onError(reason) {
        d.reject(reason);
    }
}

Based on this, here is a crude implementation of your case (watch the browser's console for output).

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure that is can help you, but try to use my code:jsfiddle

        $scope.promises = [];
        ordersByMaterial.forEach(function(order) {
            $scope.promises.push(createOrder(order));
        });

I tried to test it by creating $scope.logsCreateValue = []; where I keep random values from Math.floor(Math.random() * 10); See to the console.log($scope.logsCreateValue);, it should matches with values from $scope.orders[key].orderNumber = res[key].orderNumber;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.