1

i am trying to access multiple files from the file system using node.js. As there is a lot of data which i am trying to process and the order in which they get processed does not matter, i want to do this in asynchronous fashion using callbacks. I have written the following code;

var fs = require('fs');

var basic_path="E:\\alphaDownload\\2016\\03";
var D1 = ['\\01','\\02','\\03','\\04'];
var D2 = ['\\00','\\01','\\02','\\03','\\04','\\05','\\06','\\07','\\08','\\09','\\10','\\11','\\12','\\13','\\14','\\15','\\16','\\17','\\18','\\19','\\20','\\21','\\22','\\23'];

var path=new Array(4);
for (var i=0;i<path.length;i++){
    path[i]=new Array(24);
}

for (var j1=0; j1<D1.length; j1++){
    for (var j2=0; j2<D2.length; j2++){
            path[j1][j2]=basic_path+D1[j1]+D2[j2];

            fs.readdir(path[j1][j2], function(err,items) {
                console.log("=============="+path[j1][j2]+"================");  
                console.log(items);
                if(err){throw err;}
                else{for (var i=0; i<items.length; i++) {
                    fs.readFile(path[j1][j2]+'\\'+items[i],'utf8',function read(error, data){
                        if (error) {
                            console.log("-------**"+error);
                        }
                        else {
                            console.log(data);}
                    });

                }}
            });

    }
}

but due to asynchronous nature of readdir and readfile functions value of indexing variables [j1] and [j2] change and a single path[3][23] is passed in the readfile function.

Can anyone devise a wayout.

jazib jamil
  • 542
  • 4
  • 13

2 Answers2

0

Use async library. async.each() allows to call callbeack for a collection asynchronously and when all async functions are finished, the main calbcak will be called

UPDATE

async.map will be better in your case

Alexey
  • 980
  • 5
  • 12
0

since index var is getting overriden in each loop, you must scope the vars.

this is your case

for(var i = 0; i < 3; i++) {
  setTimeout(function(){
    console.log(i);
  }, 100);
}

and here, two approaches

for(var i = 0; i < 3; i++) {
  setTimeout(function(n){
    console.log(n);
  }.bind(this, i), 100);
}

for(var i = 0; i < 3; i++) {
  setTimeout((function(n){
    return console.log(n);
  })(i), 100);
}

Maybe this examples help you to get the idea

afmeva
  • 839
  • 7
  • 13