Javascript Array forEachSync(callback,compliteCallBack)

Description

Javascript Array forEachSync(callback,compliteCallBack)


var async = require('async');

var worker = function(array,callback,compliteCallBack){
  var index = 0;/*from  w  w w . java  2s.co m*/
  var q = async.queue(function(item,cb){
    var cback = function(){
      index = index+1;
      cb(); 
    };
    process.nextTick(function(){
      callback(item.item,index,cback);
    }); 
  },1);
  for(var i=0;i<array.length;i++){
    q.push({'item':array[i]},function(){});
  }
  q.drain = function(){
    compliteCallBack();
  };
};
Array.prototype.forEachSync = function(callback,compliteCallBack){
  new worker(this,callback,compliteCallBack);
}



PreviousNext

Related