async.js 997 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var eachLimit = function (arr, limit, iterator, callback) {
  2. callback = callback || function () {};
  3. if (!arr.length || limit <= 0) {
  4. return callback();
  5. }
  6. var completed = 0;
  7. var started = 0;
  8. var running = 0;
  9. (function replenish () {
  10. if (completed >= arr.length) {
  11. return callback();
  12. }
  13. while (running < limit && started < arr.length) {
  14. started += 1;
  15. running += 1;
  16. iterator(arr[started - 1], function (err) {
  17. if (err) {
  18. callback(err);
  19. callback = function () {};
  20. } else {
  21. completed += 1;
  22. running -= 1;
  23. if (completed >= arr.length) {
  24. callback();
  25. } else {
  26. replenish();
  27. }
  28. }
  29. });
  30. }
  31. })();
  32. };
  33. var Async = {
  34. eachLimit: eachLimit,
  35. };