1 // async-each MIT license (by Paul Miller from http://paulmillr.com).
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
9 if (items.length === 0) return callback(undefined, items);
11 var transformed = new Array(items.length);
15 items.forEach(function(item, index) {
16 next(item, function(error, transformedItem) {
20 return callback(error);
22 transformed[index] = transformedItem;
24 if (count === items.length) return callback(undefined, transformed);
29 if (typeof define !== 'undefined' && define.amd) {
30 define([], function() {
33 } else if (typeof module !== 'undefined' && module.exports) {
34 module.exports = each; // CommonJS
36 globals.asyncEach = each; // <script>