Skip to content Skip to sidebar Skip to footer

How To Make SpawnSync And Fs.readFile To Execute One After Other?

I have a python script which returns a JSON file as output by taking a FILE as input. I have 10 files, I am using spawnSync inside for loop and inside loop I have fs.readFile for r

Solution 1:

If you are rely to use third party module then I recommend to use async.eachSeries the method of the async module to resolve this issue

var filename = ['first.txt','second.txt','third.txt',....]
async.eachSeries(filename, function(item, next) {

  var myscript = spawn('python', ['/pathToPython/myPython.py', item]);
  fs.readFile('/pathToPython/' + item + '.json', 'utf8', function(err, data) {
    if (err) {
      console.log(err);
    } else {
      console.log(data);
      next();
    }
  });
})

Post a Comment for "How To Make SpawnSync And Fs.readFile To Execute One After Other?"