foreachasync.js/forEachAsync.js

36 lines
732 B
JavaScript
Raw Normal View History

2013-08-06 09:44:04 +00:00
/*jshint -W054 */
(function (exports) {
"use strict";
function forEachAsync(arr, fn, thisArg) {
var dones = []
, index = -1
;
2013-08-06 10:11:26 +00:00
function next(BREAK, result) {
2013-08-06 09:51:22 +00:00
index += 1;
if (index === arr.length || BREAK === forEachAsync.__BREAK) {
2013-08-06 09:44:04 +00:00
dones.forEach(function (done) {
2013-08-06 10:11:26 +00:00
done.call(thisArg, result);
2013-08-06 09:44:04 +00:00
});
return;
}
2013-08-06 09:51:22 +00:00
fn.call(thisArg, next, arr[index], index, arr);
2013-08-06 09:44:04 +00:00
}
setTimeout(next, 4);
return {
then: function (_done) {
dones.push(_done);
return this;
}
};
}
forEachAsync.__BREAK = {};
exports.forEachAsync = forEachAsync;
}('undefined' !== typeof exports && exports || new Function('return this')()));