diff options
Diffstat (limited to 'lib/autoInject.js')
-rw-r--r-- | lib/autoInject.js | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/lib/autoInject.js b/lib/autoInject.js index b0f5735..bc8ddd6 100644 --- a/lib/autoInject.js +++ b/lib/autoInject.js @@ -3,8 +3,10 @@ import forOwn from 'lodash/_baseForOwn'; import arrayMap from 'lodash/_arrayMap'; import isArray from 'lodash/isArray'; import trim from 'lodash/trim'; +import wrapAsync from './internal/wrapAsync'; +import { isAsync } from './internal/wrapAsync'; -var FN_ARGS = /^(function)?\s*[^\(]*\(\s*([^\)]*)\)/m; +var FN_ARGS = /^(?:async\s+)?(function)?\s*[^\(]*\(\s*([^\)]*)\)/m; var FN_ARG_SPLIT = /,/; var FN_ARG = /(=.+)?(\s*)$/; var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; @@ -38,7 +40,7 @@ function parseParams(func) { * @method * @see [async.auto]{@link module:ControlFlow.auto} * @category Control Flow - * @param {Object} tasks - An object, each of whose properties is a function of + * @param {Object} tasks - An object, each of whose properties is an {@link AsyncFunction} of * the form 'func([dependencies...], callback). The object's key of a property * serves as the name of the task defined by that property, i.e. can be used * when specifying requirements for other tasks. @@ -106,22 +108,27 @@ export default function autoInject(tasks, callback) { forOwn(tasks, function (taskFn, key) { var params; + var fnIsAsync = isAsync(taskFn); + var hasNoDeps = + (!fnIsAsync && taskFn.length === 1) || + (fnIsAsync && taskFn.length === 0); if (isArray(taskFn)) { params = taskFn.slice(0, -1); taskFn = taskFn[taskFn.length - 1]; newTasks[key] = params.concat(params.length > 0 ? newTask : taskFn); - } else if (taskFn.length === 1) { + } else if (hasNoDeps) { // no dependencies, use the function as-is newTasks[key] = taskFn; } else { params = parseParams(taskFn); - if (taskFn.length === 0 && params.length === 0) { + if (taskFn.length === 0 && !fnIsAsync && params.length === 0) { throw new Error("autoInject task functions require explicit parameters."); } - params.pop(); + // remove callback param + if (!fnIsAsync) params.pop(); newTasks[key] = params.concat(newTask); } @@ -131,7 +138,7 @@ export default function autoInject(tasks, callback) { return results[name]; }); newArgs.push(taskCb); - taskFn.apply(null, newArgs); + wrapAsync(taskFn).apply(null, newArgs); } }); |