Ulysses
Ulysses

Reputation: 6015

nodejs/js: Text not appended in file in sequence

I am trying to write state info logs in a code block that uses express, unirest and async modules as shown below.

const fsTest = require("fs");

app.post(someURL, function (req, res) {
    fsTest.appendFile("c:/fs.txt","\r\nInOf")   
    async.series({
        one: function (callback) {
                fsTest.appendFile("c:/fs.txt","\r\nInOf2")
                someOperation();        
                fsTest.appendFile("c:/fs.txt","\r\nInOf3")
                callback(false,"");         
        },
        //form search query
        two: function (callback) {
            fsTest.appendFile("c:/fs.txt","\r\nInOf4")
            anotherOperation();
            urClient.post("https://" + server + url)
                .header('Content-Type', 'application/json')             
                .send(qrySearchJSON)
                .end(
                    function (response) {
                    });
        }
    },  function (err,oResp) {
            errHandler();
        });
});

But my logs always appear out of sequence as shown below:

InOf2
InOf4
InOf3
InOf

InOf
InOf2
InOf3
InOf4

InOf
InOf2
InOf4
InOf3

InOf2
InOf
InOf4
InOf3

InOf
InOf2
InOf3
InOf4

What could possibly be the reason and what approach should i take to fix it.

Upvotes: 0

Views: 43

Answers (1)

strah
strah

Reputation: 6722

That's because fs.appendFile() is asynchronous. You, on the other hand, run it as if it was synchronous.

The easiest (not the best) solution would be to use fs.appendFileSync() - it would behave as you expect.

It would be better if you handled asynchronous nature of appendFile. Something like this:

const fsTest = require("fs");

app.post(someURL, function (req, res) {
    fsTest.appendFile("c:/fs.txt","\r\nInOf")   
    async.series({
        one: function (callback) {
                appendFile("c:/fs.txt","\r\nInOf2").then(function(){
                    someOperation();
                    return appendFile("c:/fs.txt","\r\nInOf3");
                }).then(function(){
                    callback(false,"");         
                }).catch(function(err){})
        },
        //form search query
        two: function (callback) {
            appendFile("c:/fs.txt","\r\nInOf4").then(function(){
                anotherOperation();
                urClient.post("https://" + server + url)
                    .header('Content-Type', 'application/json')             
                    .send(qrySearchJSON)
                    .end(
                        function (response) {
                        });
            }).catch(function(err){});
        }
    },  function (err,oResp) {
            errHandler();
        });
});

function appendFile(file, content) {
    var promise = new Promise(function(resolve, reject) {
        fsTest.appendFile(file, content, function(err) {
            if (err) {
                reject(err);
            } else {
                resolve();
            }
        });
    });

    return promise
}

You could use async/await, instead, or generators.
Have a look at co npm module - it does some fancy stuff.

Upvotes: 1

Related Questions