Showing posts with label NodeJS-async. Show all posts
Showing posts with label NodeJS-async. Show all posts

Friday 16 June 2017

Async module with NodeJs tutorial

Example of parallel, concat and logs example in async? List all the methods available in async

Question: What is async module?
Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.



Question: How to install async module?
npm install async




Question: How to include request module in node project?
var async = require('async');




Question: Concatenate the files from different directories?
 
async.concat(['data', 'data1'], fs.readdir, function(err, files) {
    if (err) {
        return console.log(err);
    }    
    /*files is now a list of filenames that exist in the 3 directories*/
    console.log(files);    
});




Question: What are other collection methods available in async ?
concat
concatSeries
detect
detectLimit
detectSeries
each
eachLimit
eachOf
eachOfLimit
eachOfSeries
eachSeries
every
everyLimit
everySeries
filter
filterLimit
filterSeries
groupBy
groupByLimit
groupBySeries
map
mapLimit
mapSeries
mapValues
mapValuesLimit
mapValuesSeries
reduce
reduceRight
reject
rejectLimit
rejectSeries
some
someLimit
someSeries
sortBy
transform




Question: Give an example of parallel in async?
async.parallel([
    function(callback) {
        setTimeout(function() {
            callback(null, 'one');
        }, 200);
    },
    function(callback) {
        setTimeout(function() {
            callback(null, 'two');
        }, 100);
    }
],
// optional callback
function(err, results) {
        console.log(results); //['one','two']
});




Question: What other control methods in async?
applyEach
applyEachSeries
auto
autoInject
cargo
compose
doDuring
doUntil
doWhilst
during
forever
parallel
parallelLimit
priorityQueue
queue
race
retry
retryable
seq
series
times
timesLimit
timesSeries
tryEach
until
waterfall
whilst




Question: Give an example of async.log?
var hello = function(name, callback) {
    setTimeout(function() {
        callback(null, 'hello ' + name);
    }, 1000);
};
async.log(hello, 'World 1'); //Hello World 1
async.log(hello, 'World 2'); //Hello World 2
async.log(hello, 'World 3'); //Hello World 3




Question: What other Utiles methods in async?
Utils
apply
asyncify
constant
dir
ensureAsync
log
memoize
nextTick
reflect
reflectAll
setImmediate
timeout
unmemoize