foreachasync.js/forEachAsync.js

36 lines
733 B
JavaScript
Raw Normal View History

2013-08-06 02:44:04 -07:00
/*jshint -W054 */
2014-01-13 16:06:43 -07:00
;(function (exports) {
'use strict';
2013-08-06 02:44:04 -07:00
function forEachAsync(arr, fn, thisArg) {
var dones = []
, index = -1
;
2013-08-06 03:11:26 -07:00
function next(BREAK, result) {
2013-08-06 02:51:22 -07:00
index += 1;
if (index === arr.length || BREAK === forEachAsync.__BREAK) {
2013-08-06 02:44:04 -07:00
dones.forEach(function (done) {
2013-08-06 03:11:26 -07:00
done.call(thisArg, result);
2013-08-06 02:44:04 -07:00
});
return;
}
2013-08-06 02:51:22 -07:00
fn.call(thisArg, next, arr[index], index, arr);
2013-08-06 02:44:04 -07: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')()));