Skip to content Skip to sidebar Skip to footer

Complete All Functions Inside A For Loop Before Iterating To Next Iteration Of The Loop

Say i have a function like : var bigArray = [1,2,3,4,5.........n]; for(var i=0; i

Solution 1:

The original idea came from the answer of Nina Scholz on some similar question. this answer is good if you don't like promises and deferred object. Othewise Kris Kowal's Q library would be a better choice.

functionIterator() {
  var iterator = this;
  this.queue = [];

  this.add = function(callback, wait) {
    iterator.queue.push(iterator.wait(wait));

    iterator.queue.push(function() {
      callback();
      iterator.next();
    });
  };

  this.wait = function(wait) {
    returnfunction() {
      setTimeout(iterator.next, wait);
    };
  };

  this.next = function() {
    iterator.queue.length && iterator.queue.shift()();
  };
}

var arr = [1, 2, 3, 4, 5],
  counter = -1;

var iterator = newIterator();

(functionfillNextIteration() {
  if (counter >= arr.length)
    return;
  counter++;
  iterator.add(function() {
    console.log('1 - counter value is ' + counter);
  }, 100);
  iterator.add(function() {
    console.log('2 - counter value is ' + counter);
  }, 100);
  iterator.add(function() {
    console.log('3 - counter value is ' + counter);
  }, 100);
  iterator.add(function() {
    console.log('4 - counter value is ' + counter);
  }, 100);

  iterator.add(fillNextIteration, 100);
  iterator.next();

})();

Code Explanation

Iterator class has one queue which is and array and some methods:

next

when you call next() if there is any callback in queue it will fetch first and execute that. when you call Array#shift it removes and returns first item from array. When this item is a function then you can invoke it by adding parentheses in front of it. here is the expanded version:

if (iterator.queue.length > 0) {
    var callback = iterator.queue.shift();
    callback();
}

wait

This method will add an extra callback to queue which after a timeout calls the next method. this one is for creating the expected delay.

add This method calls the wait with desired delay then attaches another function to queue which will call the callback and then calls next to make callback chain running.


fillNextIteration after explaining the iterator there is another loop here on function fillNextIteration this starts with the conditions for the first iteration for example:

if (someConditionIsTrue) {
    iterator.add(function() {
        doWhatShallBeDone;
    }, delay);
}

you can check all conditions and then if required add the callback as shown. after all condition finished add fillNextIteration as last action to continue the loop.

self invoking function As you can see fillNextIteration is self invoked. This helps to reduce one invoke and works like this:

function f(){};
f();

Last thing to mention you can create a loop by calling a function inside itself. if there is no delay or stop for revoke then it would be a deadlock. So this is another loop as well:

(function loop() { setTimeout(loop, delay); })();

Solution 2:

Try this please :

bigArray = [1, 2, 3, 4, 5.........n];
neededStuff = 10; // Number of finished works you need below
stuffOK = neededStuff;
functionmainFunction(i) {

    functionloopFunction(i) {
        if (someCondition) {
            setTimeout(function () {
                // .. do_stuff..
                stuffOK++;
                callSomeFunction();
            }, someDelayTime);
        }

        if (someCondition) {
            setTimeout(function () {
                // .. do_stuff..
                stuffOK++;
                callSomeFunction();
            }, someDelayTime);
        }

        if (someCondition) {
            setTimeout(function () {
                // .. do_stuff..
                stuffOK++;
                callSomeFunction();
            }, someDelayTime);
        }
        /*

         ...

         */if(someCondition) {
            setTimeout(function () {
                // .. do_stuff..
                stuffOK++;
                callSomeFunction();
            }, someDelayTime);
        }
    }

    if (stuffOK == neededStuff) {
        i++;
        stuffOK = 0; // reinit counterloopFunction(i);
    }

    setTimeout('mainFunction(' + i + ');', 100);

}
mainFunction(-1);

Solution 3:

Hope this will work from promise chaning

callSomeFunction1().then(function() {
   callSomeFunction2();
}).then(function() {
   callSomeFunction3();
})

Post a Comment for "Complete All Functions Inside A For Loop Before Iterating To Next Iteration Of The Loop"