Setting up Async Functionality In Node js
Posted By : Ranjan Mondal | 31-Jan-2018
Introduction : It is utility functionality by which it waits for certain function to complete after that it initiate another function.
It helps to synchronize function calling in node js.
An "async function" is asynchronous function with numbers of parameters, with the final parameter being a callback means calling function (function (arg1, arg2, ..., callback) {}). The final callback must be called once the function is completed. The callback always called with a Error as its first argument to show an error occured. If no error occurred then it should be called with null as the first argument, and any additional result arguments that may apply, to signal successful completion. The callback must be called only one time.
This type is also referred to as a "Node-style async function". The all of the methods are Node-style async functions.
Wherever we accept a Node-style async function, we also directly accept an async function. Here the async function can not be passed like call-back argument, and any error which is thrown will be used as the err argument of the automatic callback, and the return value will be used as the as final value.
Parameters:
coll is type of Array or Iterable or Object (A collection to iterate over.).
iteratee is an AsyncFunction (An async truth test to apply to each item in the collection in series. The iteratee must complete with a boolean result value. Invoked with (item, callback)).
callback is optional function (A function which called after all previous functions have finished. Result will be boolean depending on the values of the async tests.
Step 1: Install Async library.
install --save async
Step 2: import async library.
var async = require('async');
Step 3: Following code for node js
router.post('/getTransactionInfo',function(req, res, callback){
var txHashList = req.body.txHashList;
if(txHashList && txHashList.length > 0){
async.auto({
getInfoOfTranscation : function(next){
getInfoOfTranscation(txHashList,next);
},
sendTransactionInfo :['getInfoOfTranscation',function(results,next){
sendTransactionInfo(results.getInfoOfTranscation, next)
}]
},function(err,results){
callback(err,results);
});
}else {
console.log('txHashList ' + txHashList);
res.status(200).send({message: 'No data', txHashList: txHashList});
}
});
function getInfoOfTranscation(txHashList,next){
var txInfoList = [];
async.eachSeries(txHashList,function(txHash,callback){
client.getTransaction(txHash, function(err, transactionData) {
if (!err) {
console.log('confirmations is pushed for txId ' + txHash);
txInfoList.push({txHash: txHash, confirmations: transactionData.confirmations});
callback();
}else {
console.log('confirmations is not pushed for txId ' + txHash);
callback();
}
});
}, function(err, results) {
next(err, txInfoList);
});
}
function sendTransactionInfo(txInfoList,next){
console.log('data ' + JSON.stringify(txInfoList));
request.post({
headers: {'content-type' : 'application/json'},
url: config.receiveTransactionInfoList,
body: JSON.stringify({message: 'Success', status: 200, txInfoList: txInfoList})
}, function(error, response, body){
console.log('error : ' + error + ', url' + config.receiveTransactionInfoList);
next(null,null);
});
}
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
Ranjan Mondal
Ranjan is a bright Web App Developer, and has good knowledge of Core java, Spring and hibernate.