Search code examples
node.jsrace-conditionfswritefile

does fs.writeFile() handles race condition in nodejs?


Before question, I hope u have basic knowledge on aws-sqs (not quite required though).

Does fs.writeFile() handle race condition within itself ?

I have come across an implementation like ,

function getMessage(){
    sqs.receiveMessage( params , (err , data)=>{
        if(err) handle it;
        if(data.Messages){
            for(var i = 0 ;  i < data.Messages.length ; i++){
                fs.writeFile('./storage' , data.Messages[i].Body , (err)=>{
                    console.log('done');
                });
            }
        }
    })
}

for(var i = 0 ; i < 10000 ; i++){
    getMessage();
}

For those, who are confused by code, basically I am fetching messages from SQS( sort of DB query), and then after getting data, I am writing it in 'output' file. This program runs twice a day, and fetches almost half a million messages from sqs.

so if fs doesn't handle race condition, then I need to do it serially . or is there any better approach ?

I am open to any approach in nodejs :) .


Solution

  • I think you should run it serially. fs doesn't handle race condition.

    refer to this answer (https://stackoverflow.com/a/21438964/6542250).