diff options
author | Caolan McMahon <caolan@caolanmcmahon.com> | 2013-03-03 11:25:51 +0000 |
---|---|---|
committer | Caolan McMahon <caolan@caolanmcmahon.com> | 2013-03-03 11:25:51 +0000 |
commit | 82e638c909d4effb4108665918d771d08e9d8b9e (patch) | |
tree | f66a900c77ef4d9376595012f5a5bbbb980f1792 /README.md | |
parent | e265181a366552260d34d64172149f659ac797be (diff) | |
parent | 4a8099777ec6a5bc1d6fc1a2060cfb651bbd4ccc (diff) | |
download | async-82e638c909d4effb4108665918d771d08e9d8b9e.tar.gz |
Merge branch 'master' of github.com:caolan/async
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 39 |
1 files changed, 34 insertions, 5 deletions
@@ -815,13 +815,13 @@ __Example__ ```js function add1(n, callback) { - setTimeout(function () [ + setTimeout(function () { callback(null, n + 1); }, 10); } function mul3(n, callback) { - setTimeout(function () [ + setTimeout(function () { callback(null, n * 3); }, 10); } @@ -1007,14 +1007,43 @@ the functions pass an error to their callback, that function will not complete will be called immediately with the error. Functions also receive an object containing the results of functions which have completed so far. +Note, all functions are called with a results object as a second argument, +so it is unsafe to pass functions in the tasks object which cannot handle the +extra argument. For example, this snippet of code: + +```js +async.auto({ + readData: async.apply(fs.readFile, 'data.txt', 'utf-8'); +}, callback); +``` + +will have the effect of calling readFile with the results object as the last +argument, which will fail: + +```js +fs.readFile('data.txt', 'utf-8', cb, {}); +``` + +Instead, wrap the call to readFile in a function which does not forward the +results object: + +```js +async.auto({ + readData: function(cb, results){ + fs.readFile('data.txt', 'utf-8', cb); + } +}, callback); +``` + __Arguments__ * tasks - An object literal containing named functions or an array of requirements, with the function itself the last item in the array. The key used for each function or array is used when specifying requirements. The - function takes as an argument 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. + function receives two arguments: (1) 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, and (2) a results object, containing the results of + the previously executed functions. * callback(err, results) - An optional callback which is called when all the tasks have been completed. The callback will receive an error as an argument if any tasks pass an error to their callback. Results will always be passed |