index.js 500 B

123456789101112131415161718192021
  1. module.exports = function (arr, iterator, callback) {
  2. callback = callback || function () {};
  3. if (!Array.isArray(arr) || !arr.length) {
  4. return callback();
  5. }
  6. var completed = 0;
  7. var iterate = function () {
  8. iterator(arr[completed], function (err) {
  9. if (err) {
  10. callback(err);
  11. callback = function () {};
  12. }
  13. else {
  14. ++completed;
  15. if (completed >= arr.length) { callback(); }
  16. else { iterate(); }
  17. }
  18. });
  19. };
  20. iterate();
  21. };