Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for autoInject argument injection into final callback #1100

Merged
merged 11 commits into from
Apr 7, 2016
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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({
Expand All @@ -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.
Expand Down
18 changes: 17 additions & 1 deletion lib/autoInject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
22 changes: 20 additions & 2 deletions mocha_test/autoInject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
}]);
});

});