Write File with Sync and Async using Node.js



Writing the file using file system module. we have two options to process synchronous and asynchronous. Here we are testing on synchronous of the node.js. The sequence of code should wait until we write the sample txt file. For that waiting we are using writeFileSync class from File system module.

var file = require("fs");
console.log("Starting...");

var contents = file.writeFileSync("./write_file.txt", "Text to Write");
console.log(" done ...");

Here we are testing on asynchronous of the node.js. The sequence of code should not wait until we read the sample txt file. For that we are using callback function.

var file = require("fs");

console.log("Starting...");

var contents = file.writeFile("./write_file.txt", "Text to Write",
function(error){
console.log("written file");
}
);

console.log(" done ...");

12 comments:

  1. Re:
    "The sequence of code should not wait until we read the sample txt file."
    Read?? or Write?

    ReplyDelete
  2. Thanks for sharing this information. Its really useful detail about Nodejs. Nodejs Training in Bangalore

    ReplyDelete
  3. This is a nice and informative, containing all information and also has a great impact on the new technology.
    node.js development company

    ReplyDelete
  4. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's. 
    best rpa training in bangalore
    rpa training in bangalore | rpa course in bangalore
    RPA training in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  5. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  6. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    AWS Training in Bangalore

    AWS Training in pune

    ReplyDelete
  7. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles learn Node JS training

    ReplyDelete
  8. Nice article I was impressed by seeing this blog, it was very interesting and it is Thanks for sharing all the information with us all.very useful for meThank you for sharing any good knowledge and thanks for fantastic efforts. Really the post is very unique.every concepts are captured nice.
    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    hadoop training in chennai

    hadoop training in bangalore

    ReplyDelete