Implementing the Iteration in NodeJS with interdependent results
Posted By : RAJAT KATIYAR | 27-Dec-2016
There are times when there is a need to implement loop where the result in every iteration is required in the next one, but due to asynchronous behaviour of the NodeJS, it is difficult to implement it when there are some time consuming computation in every iteration. Due to which during the time of execution NodeJs doesn’t waits for the process to get complete and moves on and consequently the result is lost.
So for implementing this type of iterations in NodeJS, one way could be is to use the recursive function.
Recursive function is that function which keeps on calling itself until the condition is not fulfilled.
For Example:
Let’s say there is a recursive function:
var recursiveFunction = function(params){
async.auto({
getTimeConsumingResults:function(next,result){
highComputationFn(params,next)
}
},function(err,results){
if(results.getTimeConsumingResults.condition){
recursiveFunction(results.getTimeConsumingResults)
}else{
console.log("The Results Successfully Achieved")
}
}
})
}
So, in this way the iterations can be implemented with the desired results.
More From Oodles
Ready to innovate? Let's get in touch
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
RAJAT KATIYAR
Rajat Katiyar is a very bright web app developer, he has good knowledge of JAVA, C/C++, C#.NET. Apart from this he has interest in theater and sports.