简体   繁体   中英

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?

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM