0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

hi @strah - i found that as well - but could you suggest some better options
I have added some sample code (not tested, though).

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.