summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2016-03-09 13:06:28 -0800
committerAlexander Early <alexander.early@gmail.com>2016-03-09 13:06:28 -0800
commitdf31042d689ba51e383209b2836ad971c11dee07 (patch)
tree7714cf6b84b2e6efea44d5a7216e2383baf085f5
parent27230c22ccc567f7944b0dc7be23148c5e7304b0 (diff)
downloadasync-autoinject.tar.gz
remove ES6 idiomsautoinject
-rw-r--r--README.md6
-rw-r--r--lib/autoInject.js13
2 files changed, 8 insertions, 11 deletions
diff --git a/README.md b/README.md
index fab0a74..b872bc8 100644
--- a/README.md
+++ b/README.md
@@ -1470,19 +1470,15 @@ async.autoInject({
});
```
-If you are using a minifier that mangles parameter names, `autoInject` will not work with plain functions. To work around this, you can explicitly specify the names of the parameters in an array, similar to Angular.js dependency injection.
+If you are using a JS minifier that mangles parameter names, `autoInject` will not work with plain functions, since the parameter names will be collapsed to a single letter identifier. To work around this, you can explicitly specify the names of the parameters your task function needs in an array, similar to Angular.js dependency injection.
```js
async.autoInject({
//...
write_file: ['get_data', 'make_folder', function(get_data, make_folder, callback){
- // once there is some data and the directory exists,
- // write the data to a file in the directory
callback(null, 'filename');
}],
email_link: ['write_file', function(write_file, callback){
- // once the file is written let's email a link to it...
- // write_file contains the filename returned by write_file.
callback(null, {'file':write_file, 'email':'user@example.com'});
}]
//...
diff --git a/lib/autoInject.js b/lib/autoInject.js
index 1b6fea4..f84a4d9 100644
--- a/lib/autoInject.js
+++ b/lib/autoInject.js
@@ -1,6 +1,7 @@
import auto from './auto';
import forOwn from 'lodash/forOwn';
import arrayMap from 'lodash/_arrayMap';
+import clone from 'lodash/_baseClone';
import isArray from 'lodash/isArray';
var argsRegex = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
@@ -16,28 +17,28 @@ export default function autoInject(tasks, callback) {
var params;
if (isArray(taskFn)) {
- params = [...taskFn];
+ params = clone(taskFn);
taskFn = params.pop();
- newTasks[key] = [...params].concat(newTask);
+ newTasks[key] = clone(params).concat(newTask);
} else if (taskFn.length === 0) {
throw new Error("autoInject task functions require explicit parameters.");
} else if (taskFn.length === 1) {
- // no dependencies
+ // no dependencies, use the function as-is
newTasks[key] = taskFn;
} else {
params = parseParams(taskFn);
params.pop();
- newTasks[key] = [...params].concat(newTask);
-
+ newTasks[key] = clone(params).concat(newTask);
}
function newTask(results, taskCb) {
var newArgs = arrayMap(params, function (name) {
return results[name];
});
- taskFn(...newArgs.concat(taskCb));
+ newArgs.push(taskCb);
+ taskFn.apply(null, newArgs);
}
});