summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Early <alexander.early@gmail.com>2016-04-07 13:47:21 -0700
committerAlex Early <alexander.early@gmail.com>2016-04-07 13:47:21 -0700
commitd77e19a59a9aac088cae964507cd56f743ccc834 (patch)
tree0438f52aecc300a60fa2c2e2d27e42cbc1ec78fd
parent6ad036d07add9aa24a26b831cd211041d3b681f6 (diff)
parent3e33843000cba809a52364f10cbb48b385025b46 (diff)
downloadasync-d77e19a59a9aac088cae964507cd56f743ccc834.tar.gz
Merge pull request #1100 from steverobb/autoInjectPR
Fix for autoInject argument injection into final callback
-rw-r--r--README.md9
-rw-r--r--lib/autoInject.js18
-rw-r--r--mocha_test/autoInject.js22
3 files changed, 43 insertions, 6 deletions
diff --git a/README.md b/README.md
index 77c14ce..0d66889 100644
--- a/README.md
+++ b/README.md
@@ -1498,7 +1498,7 @@ __Arguments__
* `tasks` - An object, each of whose properties is a function 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.
* The `callback` parameter is a `callback(err, result)` which must be called when finished, passing an `error` (which can be `null`) and the result of the function's execution. The remaining parameters name other tasks on which the task is dependent, and the results from those tasks are the arguments of those parameters.
-* `callback(err, [results...])` - An optional callback which is called when all the tasks have been completed. It receives the `err` argument if any `tasks` pass an error to their callback. The remaining parameters are task names whose results you are interested in. This callback will only be called when all tasks have finished or an error has occurred, and so do not not specify dependencies in the same way as `tasks` do. If an error occurs, no further `tasks` will be performed, and `results` will only be valid for those tasks which managed to complete.
+* `callback(err, [results...])` - An optional callback which is called when all the tasks have been completed. It receives the `err` argument if any `tasks` pass an error to their callback. The remaining parameters are task names whose results you are interested in. This callback will only be called when all tasks have finished or an error has occurred, and so do not specify dependencies in the same way as `tasks` do. If an error occurs, no further `tasks` will be performed, and `results` will only be valid for those tasks which managed to complete.
__Example__
@@ -1532,7 +1532,7 @@ async.autoInject({
});
```
-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.
+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. The final results callback can be provided as an array in the same way.
```js
async.autoInject({
@@ -1544,7 +1544,10 @@ async.autoInject({
callback(null, {'file':write_file, 'email':'user@example.com'});
}]
//...
-}, done);
+}, ['email_link', function(err, email_link) {
+ console.log('err = ', err);
+ console.log('email_link = ', email_link);
+}]);
```
This still has an advantage over plain `auto`, since the results a task depends on are still spread into arguments.
diff --git a/lib/autoInject.js b/lib/autoInject.js
index 9259487..245db8e 100644
--- a/lib/autoInject.js
+++ b/lib/autoInject.js
@@ -42,5 +42,21 @@ export default function autoInject(tasks, callback) {
}
});
- auto(newTasks, callback);
+ auto(newTasks, function (err, results) {
+ var params;
+ if (isArray(callback)) {
+ params = clone(callback);
+ callback = params.pop();
+ } else {
+ params = parseParams(callback);
+ params.shift();
+ }
+
+ params = arrayMap(params, function (name) {
+ return results[name];
+ });
+
+ params.unshift(err);
+ callback.apply(null, params);
+ });
}
diff --git a/mocha_test/autoInject.js b/mocha_test/autoInject.js
index 059c7ae..1408af9 100644
--- a/mocha_test/autoInject.js
+++ b/mocha_test/autoInject.js
@@ -44,8 +44,8 @@ describe('autoInject', function () {
callback(null, 6);
}
},
- function(err, results){
- expect(results.task6).to.equal(6);
+ function(err, task6){
+ expect(task6).to.equal(6);
expect(callOrder).to.eql(['task2','task3','task6','task5','task1','task4']);
done();
});
@@ -74,4 +74,22 @@ describe('autoInject', function () {
});
});
+ it('should work with array results', function (done) {
+ async.autoInject({
+ task1: function (cb) {
+ cb(null, 1);
+ },
+ task2: function (task3, cb) {
+ cb(null, 2);
+ },
+ task3: function (cb) {
+ cb(null, 3);
+ }
+ }, ['task3', 'task1', function (err, task3, task1) {
+ expect(task1).to.equal(1);
+ expect(task3).to.equal(3);
+ done();
+ }]);
+ });
+
});