1

I am trying to run two functions on javascript node.js one after the other These are the two functions

This functions runs a bat file

function loadTime() {
    var exec = require('child_process').exec;
    exec('C:\\Temp\\tasks\\acis\\runme.bat', function(error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        if (error !== null) {
            console.log('exec error: ' + error);
        }
    });
}

I have to wait for the result of this bat file to run the next function, which is

function parseTime() {
    var parser = new xml2js.Parser();
    fs.readFile('C:\\Temp\\tasks\\acis\\2.sat\\2.sat.response.xml', function(err, data) {
        parser.parseString(data, function(err, result) {
            var timeString = result.Message.Response[0].Events[0].MessageReportEvent[8].$.Message;
            var fileTime = timeString.substr(13, 20);
            console.log(fileTime);
        });
    });
}

But in javascript when I run this as

loadTime();
parseTime();

It seems parseTime() function starts to run before loadTime() finishes running. How do I run parseTime() after loadTime() is finished?

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
TMA
  • 111
  • 1
  • 2
  • 9

4 Answers4

1

either you can call parseTime() method inside loadTime() so that once loadTime() completes then only parseTime() is called. Else you can use async module of nodejs to accomplish this.

Abie
  • 624
  • 5
  • 12
1

function loadTime(done) {

var exec = require('child_process').exec;
exec('C:\\Temp\\tasks\\acis\\runme.bat', function(error, stdout, stderr) {
    console.log('stdout: ' + stdout);

    if (error !== null) {
        console.log('exec error: ' + error);
    }

    done();
});


function parseTime() {

    var parser = new xml2js.Parser();
    fs.readFile('C:\\Temp\\tasks\\acis\\2.sat\\2.sat.response.xml', function(err, data) {

        parser.parseString(data, function(err, result) {

            var timeString = result.Message.Response[0].Events[0].MessageReportEvent[8].$.Message;
            var fileTime = timeString.substr(13, 20);

            console.log(fileTime);


        });

    });

};


loadTime(
    function() {
        parseTime();
    }
);
Hiren S.
  • 2,793
  • 15
  • 25
1

You could simply give loadTime() a callback function that executes once it's finished loading, and have this callback function be your parseTime()

Something like this might work (I haven't tested it)

function loadTime(callback){
    var exec = require('child_process').exec;
    exec('C:\\Temp\\tasks\\acis\\runme.bat', function(error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        if (error !== null) {
        console.log('exec error: ' + error);
        }else{
            // Assuming you want parseTime() to fire if there were no errors in loadTime()
            // When we reach this point, we want to fire our callback function
            callback();
        }
    }
);

function parseTime(){
    var parser = new xml2js.Parser();
    fs.readFile('C:\\Temp\\tasks\\acis\\2.sat\\2.sat.response.xml', function(err, data) {
        parser.parseString(data, function (err, result) {
            var timeString = result.Message.Response[0].Events[0].MessageReportEvent[8].$.Message;
            var fileTime = timeString.substr(13,20);
            console.log(fileTime);
         });
     });
};

// We call loadTime() with parseTime() as its callback
loadTime(function(){
    parseTime();
});
Nick Zuber
  • 5,467
  • 3
  • 24
  • 48
1

You can set up your functions to accept callbacks like this:

function first(callback) {
    console.log("First!");
    callback();
}
function second() {
    console.log("Second!");
}
first(second);

Should output to console: First! Second!

You'll more than likely want to be able to make callback optional so it won't error if it's not passed:

if ( callback ) callback();

jaggedsoft
  • 3,858
  • 2
  • 33
  • 41