diff options
author | Alexander Early <alexander.early@gmail.com> | 2015-05-27 23:57:42 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2015-05-27 23:57:42 -0700 |
commit | b11fc31a418eb00f1bcadb878f37551e74f97c38 (patch) | |
tree | c43f4494723fdf03a880b6441d4eb4b95a074b32 /lib | |
parent | d35f46067805248be4127030cedda0a80f80b938 (diff) | |
download | async-b11fc31a418eb00f1bcadb878f37551e74f97c38.tar.gz |
more style changes
Diffstat (limited to 'lib')
-rw-r--r-- | lib/async.js | 116 |
1 files changed, 61 insertions, 55 deletions
diff --git a/lib/async.js b/lib/async.js index 1fc920c..38f2a4c 100644 --- a/lib/async.js +++ b/lib/async.js @@ -178,7 +178,7 @@ return callback(null); } var completed = 0; - var iterate = function () { + function iterate() { iterator(arr[completed], function (err) { if (err) { callback(err); @@ -194,7 +194,7 @@ } } }); - }; + } iterate(); }; @@ -281,7 +281,7 @@ return callback(); } var completed = 0; - var iterate = function () { + function iterate() { var sync = true; var key = keys[completed]; iterator(obj[key], key, function (err) { @@ -305,7 +305,7 @@ } }); sync = false; - }; + } iterate(); }; @@ -549,15 +549,17 @@ return callback(err); } else { - var fn = function (left, right) { - var a = left.criteria, b = right.criteria; - return a < b ? -1 : a > b ? 1 : 0; - }; - callback(null, _map(results.sort(fn), function (x) { + callback(null, _map(results.sort(comparator), function (x) { return x.value; })); } + }); + + function comparator(left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + } }; async.auto = function (tasks, callback) { @@ -571,23 +573,23 @@ var results = {}; var listeners = []; - var addListener = function (fn) { + function addListener(fn) { listeners.unshift(fn); - }; - var removeListener = function (fn) { + } + function removeListener(fn) { for (var i = 0; i < listeners.length; i += 1) { if (listeners[i] === fn) { listeners.splice(i, 1); return; } } - }; - var taskComplete = function () { + } + function taskComplete() { remainingTasks--; _each(listeners.slice(0), function (fn) { fn(); }); - }; + } addListener(function () { if (!remainingTasks) { @@ -601,7 +603,7 @@ _each(keys, function (k) { var task = _isArray(tasks[k]) ? tasks[k]: [tasks[k]]; - var taskCallback = function (err) { + function taskCallback(err) { var args = _baseSlice(arguments, 1); if (args.length <= 1) { args = args[0]; @@ -620,7 +622,7 @@ results[k] = args; async.setImmediate(taskComplete); } - }; + } var requires = task.slice(0, Math.abs(task.length - 1)) || []; // prevent dead-locks var len = requires.length; @@ -633,23 +635,23 @@ throw new Error('Has cyclic dependencies'); } } - var ready = function () { + function ready() { return _reduce(requires, function (a, x) { return (a && results.hasOwnProperty(x)); }, true) && !results.hasOwnProperty(k); - }; + } if (ready()) { task[task.length - 1](taskCallback, results); } else { - var listener = function () { - if (ready()) { - removeListener(listener); - task[task.length - 1](taskCallback, results); - } - }; addListener(listener); } + function listener() { + if (ready()) { + removeListener(listener); + task[task.length - 1](taskCallback, results); + } + } }); }; @@ -664,14 +666,16 @@ } // Make sure times is a number times = parseInt(times, 10) || DEFAULT_TIMES; - var wrappedTask = function(wrappedCallback, wrappedResults) { - var retryAttempt = function(task, finalAttempt) { + + function wrappedTask(wrappedCallback, wrappedResults) { + function retryAttempt(task, finalAttempt) { return function(seriesCallback) { task(function(err, result){ seriesCallback(!err || finalAttempt, {err: err, result: result}); }, wrappedResults); }; - }; + } + while (times) { attempts.push(retryAttempt(task, !(times-=1))); } @@ -679,7 +683,8 @@ data = data[data.length - 1]; (wrappedCallback || callback)(data.err, data.result); }); - }; + } + // If a callback is passed, run this as a controll flow return callback ? wrappedTask() : wrappedTask; }; @@ -693,7 +698,7 @@ if (!tasks.length) { return callback(); } - var wrapIterator = function (iterator) { + function wrapIterator(iterator) { return function (err) { if (err) { callback.apply(null, arguments); @@ -713,11 +718,11 @@ }); } }; - }; + } wrapIterator(async.iterator(tasks))(); }; - var _parallel = function(eachfn, tasks, callback) { + function _parallel(eachfn, tasks, callback) { callback = callback || noop; if (_isArray(tasks)) { eachfn.map(tasks, function (fn, callback) { @@ -747,7 +752,7 @@ callback(err, results); }); } - }; + } async.parallel = function (tasks, callback) { _parallel({ map: async.map, each: async.each }, tasks, callback); @@ -790,18 +795,18 @@ }; async.iterator = function (tasks) { - var makeCallback = function (index) { - var fn = function () { + function makeCallback(index) { + function fn() { if (tasks.length) { tasks[index].apply(null, arguments); } return fn.next(); - }; + } fn.next = function () { return (index < tasks.length - 1) ? makeCallback(index + 1): null; }; return fn; - }; + } return makeCallback(0); }; @@ -814,7 +819,7 @@ }; }; - var _concat = function (eachfn, arr, fn, callback) { + function _concat(eachfn, arr, fn, callback) { var r = []; eachfn(arr, function (x, cb) { fn(x, function (err, y) { @@ -824,7 +829,7 @@ }, function (err) { callback(err, r); }); - }; + } async.concat = doParallel(_concat); async.concatSeries = doSeries(_concat); @@ -953,19 +958,20 @@ q.empty(); } workers += 1; - var next = function () { - workers -= 1; - if (task.callback) { - task.callback.apply(task, arguments); - } - if (q.drain && q.tasks.length + workers === 0) { - q.drain(); - } - q.process(); - }; var cb = only_once(next); worker(task.data, cb); } + + function next() { + workers -= 1; + if (task.callback) { + task.callback.apply(task, arguments); + } + if (q.drain && q.tasks.length + workers === 0) { + q.drain(); + } + q.process(); + } }, length: function () { return q.tasks.length; @@ -1127,7 +1133,7 @@ return cargo; }; - var _console_fn = function (name) { + function _console_fn(name) { return function (fn) { var args = _baseSlice(arguments, 1); fn.apply(null, args.concat([function (err) { @@ -1146,7 +1152,7 @@ } }])); }; - }; + } async.log = _console_fn('log'); async.dir = _console_fn('dir'); /*async.info = _console_fn('info'); @@ -1159,7 +1165,7 @@ hasher = hasher || function (x) { return x; }; - var memoized = function () { + function memoized() { var args = _baseSlice(arguments); var callback = args.pop(); var key = hasher.apply(null, args); @@ -1182,7 +1188,7 @@ } }])); } - }; + } memoized.memo = memo; memoized.unmemoized = fn; return memoized; @@ -1234,7 +1240,7 @@ }; function _applyEach(eachfn, fns /*args...*/) { - var go = function () { + function go() { var that = this; var args = _baseSlice(arguments); var callback = args.pop(); @@ -1242,7 +1248,7 @@ fn.apply(that, args.concat([cb])); }, callback); - }; + } if (arguments.length > 2) { var args = _baseSlice(arguments, 2); return go.apply(this, args); |