Understanding async.auto in node js
Posted By : Ravi Verma | 30-Jun-2015
The best way for running the functions in tasks
is determined as auto(tasks, [callback]) ,which is requirement based for the functions. Each function are dependent on other , and each and every function run when their requirements are fullfilled.
If error is passed in any function to its callback
, it will not complete and if any other functions dependent on the same function then it will not run, and the main callback will immediately called with the error. Functions also get an object containing the results of those functions which have completed till now.
Note, results
Objects are counted as a second argument when all functions are called, so it is unsafe to pass functions in the tasks
object that may handle the extra argument.
For example, this snippet of code:
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:
fs.readFile('data.txt', 'utf-8', cb, {});
Instead, wrap the call to readFile
in a function which does not forward the results
object:
async.auto({
readData: function(cb, results){
fs.readFile('data.txt', 'utf-8', cb);
}
}, callback);
Arguments
tasks
-Tasks is basically An object which is a last item in the array with the function itself with all its properties wheather a function or an array of requirements. That property defined the name of the task which is servered by the object's key, i.e. may be used when specifying requirements for other tasks. The functions receive two arguments: (1) acallback(err, result)
which must be called when finished, passing an error (which can benull
) and the result of the function's execution, and (2) aresults
object which is the previously executed functions containing the results .callback(err, results)
- An optional callback is called when all the tasks completed .If anytasks
passed an error to their callback It receives theerr
argument . if an error occurs, no furthertasks
will be performed, and the results object will only contain partial results as Results are always returned.
Example
async.auto({
get_data: function(callback){
console.log('in get_data');
// async code to get some data
callback(null, 'data', 'converted to array');
},
make_folder: function(callback){
console.log('in make_folder');
// async code to create a directory to store a file in
// this is run at the same time as getting the data
callback(null, 'folder');
},
write_file: ['get_data', 'make_folder', function(callback, results){
console.log('in write_file', JSON.stringify(results));
// 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(callback, results){
console.log('in email_link', JSON.stringify(results));
// once the file is written let's email a link to it...
// results.write_file contains the filename returned by write_file.
callback(null, {'file':results.write_file, 'email':'[email protected]'});
}]
}, function(err, results) {
console.log('err = ', err);
console.log('results = ', results);
});
Hope you find it helpful.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ravi Verma
Ravi is a seasoned technologist with excellent experience in AngularJS , NodeJS and MEAN stack. He has good experience in developing complex UIs for web and mobile applications.