Read file synch inside read directory sync does not wait before compile next line

60 Views Asked by At

I have this code;

Where I read a directory sync and for each file in that directory Im reading the data sync as well.

fs.readdirSync(folder).forEach(file => {
       try{
            fs.readFileSync(folder+file, {encoding: 'utf-8'}, function(error, data) {
                    parser.parseString(data, function(err, res) {
                        if (err){
                            console.log("Error in " + file +" " + err)
                        }
                        else{
                            console.log("Success")

                        }                       
                        
                    });
               
               });
            }
            catch(e){
                console.log(e)
           }
            })
 var q = await doSomething();                                                                               

doSomething() executes before the sync read is done. Is this expected behaviour? My understanding was that sync methods would block off any further compiling until it was done.

Im fairly new to Nodejs synch and asynch.

0

There are 0 best solutions below