Javascript Array asyncForEach(cb, done)

Description

Javascript Array asyncForEach(cb, done)


Array.prototype.asyncForEach = function (cb, done) {
  var total = this.length;
  var result = [];

  if(total == 0) {
    return done(result);
  }//from  w w w.ja  v a2  s .  c  o m

  this.forEach(function (element, i) {
    setTimeout(function () {
      cb(element, function (item) {
        result[i] = item;

        if (--total == 0) {
          done(result);
        }
      });
    });
  });
};



PreviousNext

Related