TMA
TMA

Reputation: 111

Running two functions on javascript node.js one after the other

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?

Upvotes: 1

Views: 942

Answers (4)

Nick Zuber
Nick Zuber

Reputation: 5637

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();
});

Upvotes: 1

jaggedsoft
jaggedsoft

Reputation: 4038

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();

Upvotes: 1

Hiren S.
Hiren S.

Reputation: 2832

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();
    }
);

Upvotes: 1

Abie
Abie

Reputation: 644

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.

Upvotes: 1

Related Questions