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?

2
  • 1
    Make loadTime accept a callback and call that callback when the function is done. Commented Nov 18, 2015 at 6:29
  • 1
    See Create a custom callback in JavaScript Commented Nov 18, 2015 at 6:34

4 Answers 4

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();
});
Sign up to request clarification or add additional context in comments.

2 Comments

Also you can do loadTime(parseTime);
@Ziki Yeah I believe that works too; I guess it's just preference
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.

Comments

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

2 Comments

Just posting code without any explanation is not really helpful for a beginner.
Also you can do loadTime(parseTime);
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.