Javascript Array asyncForEach(cb, finishedCb)

Description

Javascript Array asyncForEach(cb, finishedCb)


Array.prototype.asyncForEach = function(cb, finishedCb){
  var _this = this;

  function asyncCall(index){
    setTimeout(function(){
      cb(_this[index], index, _this);/*  ww w.  ja v a2s. c om*/
      index++;
      if (index < _this.length){
        asyncCall(index);
      } else {
        finishedCb(_this);
      }
    }, 0);
  }

  asyncCall(0);
}



PreviousNext

Related