index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // async-each MIT license (by Paul Miller from https://paulmillr.com).
  2. (function(globals) {
  3. 'use strict';
  4. var each = function(items, next, callback) {
  5. if (!Array.isArray(items)) throw new TypeError('each() expects array as first argument');
  6. if (typeof next !== 'function') throw new TypeError('each() expects function as second argument');
  7. if (typeof callback !== 'function') callback = Function.prototype; // no-op
  8. if (items.length === 0) return callback(undefined, items);
  9. var transformed = new Array(items.length);
  10. var count = 0;
  11. var returned = false;
  12. items.forEach(function(item, index) {
  13. next(item, function(error, transformedItem) {
  14. if (returned) return;
  15. if (error) {
  16. returned = true;
  17. return callback(error);
  18. }
  19. transformed[index] = transformedItem;
  20. count += 1;
  21. if (count === items.length) return callback(undefined, transformed);
  22. });
  23. });
  24. };
  25. if (typeof define !== 'undefined' && define.amd) {
  26. define([], function() {
  27. return each;
  28. }); // RequireJS
  29. } else if (typeof module !== 'undefined' && module.exports) {
  30. module.exports = each; // CommonJS
  31. } else {
  32. globals.asyncEach = each; // <script>
  33. }
  34. })(this);