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
|
|
9
|
if (items.length === 0) return callback(undefined, items);
|
10
|
|
11
|
var transformed = new Array(items.length);
|
12
|
var count = 0;
|
13
|
var returned = false;
|
14
|
|
15
|
items.forEach(function(item, index) {
|
16
|
next(item, function(error, transformedItem) {
|
17
|
if (returned) return;
|
18
|
if (error) {
|
19
|
returned = true;
|
20
|
return callback(error);
|
21
|
}
|
22
|
transformed[index] = transformedItem;
|
23
|
count += 1;
|
24
|
if (count === items.length) return callback(undefined, transformed);
|
25
|
});
|
26
|
});
|
27
|
};
|
28
|
|
29
|
if (typeof define !== 'undefined' && define.amd) {
|
30
|
define([], function() {
|
31
|
return each;
|
32
|
}); // RequireJS
|
33
|
} else if (typeof module !== 'undefined' && module.exports) {
|
34
|
module.exports = each; // CommonJS
|
35
|
} else {
|
36
|
globals.asyncEach = each; // <script>
|
37
|
}
|
38
|
})(this);
|