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.

 

About Author

Author Image
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.

Request for Proposal

Name is required

Comment is required

Sending message..