filer/lib/async.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-02-18 19:51:06 +00:00
/*global setImmediate: false, setTimeout: false, console: false */
/**
2014-09-09 16:53:12 +00:00
* async.js shim, based on https://raw.github.com/caolan/async/master/lib/async.js Feb 18, 2014
2014-02-18 19:51:06 +00:00
* Used under MIT - https://github.com/caolan/async/blob/master/LICENSE
*/
(function () {
var async = {};
2014-09-09 16:53:12 +00:00
// async.js functions used in Filer
2014-02-18 19:51:06 +00:00
2014-09-09 17:10:30 +00:00
//// nextTick implementation with browser-compatible fallback ////
2014-02-18 19:51:06 +00:00
if (typeof process === 'undefined' || !(process.nextTick)) {
if (typeof setImmediate === 'function') {
async.nextTick = function (fn) {
// not a direct alias for IE10 compatibility
setImmediate(fn);
};
async.setImmediate = async.nextTick;
}
else {
async.nextTick = function (fn) {
setTimeout(fn, 0);
};
async.setImmediate = async.nextTick;
}
}
else {
async.nextTick = process.nextTick;
if (typeof setImmediate !== 'undefined') {
async.setImmediate = function (fn) {
// not a direct alias for IE10 compatibility
setImmediate(fn);
};
}
else {
async.setImmediate = async.nextTick;
}
}
async.eachSeries = function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length) {
return callback();
}
var completed = 0;
var iterate = function () {
iterator(arr[completed], function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
if (completed >= arr.length) {
2014-09-09 16:53:12 +00:00
callback();
2014-02-18 19:51:06 +00:00
}
else {
iterate();
}
}
});
};
iterate();
};
async.forEachSeries = async.eachSeries;
// AMD / RequireJS
if (typeof define !== 'undefined' && define.amd) {
define([], function () {
return async;
});
}
// Node.js
else if (typeof module !== 'undefined' && module.exports) {
module.exports = async;
}
// included directly via <script> tag
else {
root.async = async;
}
}());