I want to handle nodejs
asynchronous issue.
please help me with a sweet example - it ll be better for me if you able to do it by callback or like callback related thing.
Thanks
I want to handle nodejs
asynchronous issue.
please help me with a sweet example - it ll be better for me if you able to do it by callback or like callback related thing.
Thanks
Sample examples
Foreach loop
var fs = require('fs')
var paths = ['/home' , '/root', '/var']
paths.forEach(function( path ) {
fs.lstat( path, function(err, stat) {
console.log( path, stat );
});
});
For loop
for (var i = 0, c = paths.length; i < c; i++)
{
// creating an Immiedately Invoked Function Expression
(function( path ) {
fs.lstat(path, function (error, stat) {
console.log(path, stat);
});
})( paths[i] );
// passing paths[i] in as "path" in the closure
}
Recursion
function iteratePath(paths, i, max){
if(i<max) {
return;
}else{
fs.lstat( path, function(err, stat) {
console.log( path, stat );
//Recursive call back
iteratePath(paths, i, max)
});
}
}
var paths = ['/home' , '/root', '/var']
iteratePath(paths,0, size)
You can use async Using async/await with a forEach loop