Understanding fs.writeFileSync(path, data[, options]) Node.js












0















I have a function that writes data to a file then uploads that file to cloud storage. The file isn't finished writing before it starts uploading so I am getting a partial file in cloud storage. I found that fs.writeFileSync(path, data[, options]) could help, but I am not exactly sure how it works.



It is my understanding that node runs asynchronously and I have several async processes running prior to this portion of code. I understand what synchronous vs asynchronous means, but I am having a little trouble understanding how it plays in this example. Here are my questions if I replace the below code with fs.writeFileSync(path, data[, options])




  1. What do the docs mean by "Synchronously append data to a file"


    • a. Will the next lines of code be halted until the fs.writeFileSync(path, data) is finished?

    • b. Are previous asynchronous processes halted by this line of code?



  2. If other async processes are not affected how is writeFileSync different the writeFile?


  3. Is there a callback feature in writeFileSync that I am misunderstanding?



Code for reference



outCsv = "x","y","z"
filename = "file.csv"
fs.writeFile(filename, outCsv, function (err) {
if (err) {
return console.log(err);
}
console.log('The file was saved!');
bucket.upload(filename, (err, file) => {
if (err) {
return console.log(err);
}
console.log('The file was uploaded!');
});
});









share|improve this question

























  • Thanks, jonrsharpe for the edit

    – lukechambers91
    Nov 15 '18 at 12:35
















0















I have a function that writes data to a file then uploads that file to cloud storage. The file isn't finished writing before it starts uploading so I am getting a partial file in cloud storage. I found that fs.writeFileSync(path, data[, options]) could help, but I am not exactly sure how it works.



It is my understanding that node runs asynchronously and I have several async processes running prior to this portion of code. I understand what synchronous vs asynchronous means, but I am having a little trouble understanding how it plays in this example. Here are my questions if I replace the below code with fs.writeFileSync(path, data[, options])




  1. What do the docs mean by "Synchronously append data to a file"


    • a. Will the next lines of code be halted until the fs.writeFileSync(path, data) is finished?

    • b. Are previous asynchronous processes halted by this line of code?



  2. If other async processes are not affected how is writeFileSync different the writeFile?


  3. Is there a callback feature in writeFileSync that I am misunderstanding?



Code for reference



outCsv = "x","y","z"
filename = "file.csv"
fs.writeFile(filename, outCsv, function (err) {
if (err) {
return console.log(err);
}
console.log('The file was saved!');
bucket.upload(filename, (err, file) => {
if (err) {
return console.log(err);
}
console.log('The file was uploaded!');
});
});









share|improve this question

























  • Thanks, jonrsharpe for the edit

    – lukechambers91
    Nov 15 '18 at 12:35














0












0








0








I have a function that writes data to a file then uploads that file to cloud storage. The file isn't finished writing before it starts uploading so I am getting a partial file in cloud storage. I found that fs.writeFileSync(path, data[, options]) could help, but I am not exactly sure how it works.



It is my understanding that node runs asynchronously and I have several async processes running prior to this portion of code. I understand what synchronous vs asynchronous means, but I am having a little trouble understanding how it plays in this example. Here are my questions if I replace the below code with fs.writeFileSync(path, data[, options])




  1. What do the docs mean by "Synchronously append data to a file"


    • a. Will the next lines of code be halted until the fs.writeFileSync(path, data) is finished?

    • b. Are previous asynchronous processes halted by this line of code?



  2. If other async processes are not affected how is writeFileSync different the writeFile?


  3. Is there a callback feature in writeFileSync that I am misunderstanding?



Code for reference



outCsv = "x","y","z"
filename = "file.csv"
fs.writeFile(filename, outCsv, function (err) {
if (err) {
return console.log(err);
}
console.log('The file was saved!');
bucket.upload(filename, (err, file) => {
if (err) {
return console.log(err);
}
console.log('The file was uploaded!');
});
});









share|improve this question
















I have a function that writes data to a file then uploads that file to cloud storage. The file isn't finished writing before it starts uploading so I am getting a partial file in cloud storage. I found that fs.writeFileSync(path, data[, options]) could help, but I am not exactly sure how it works.



It is my understanding that node runs asynchronously and I have several async processes running prior to this portion of code. I understand what synchronous vs asynchronous means, but I am having a little trouble understanding how it plays in this example. Here are my questions if I replace the below code with fs.writeFileSync(path, data[, options])




  1. What do the docs mean by "Synchronously append data to a file"


    • a. Will the next lines of code be halted until the fs.writeFileSync(path, data) is finished?

    • b. Are previous asynchronous processes halted by this line of code?



  2. If other async processes are not affected how is writeFileSync different the writeFile?


  3. Is there a callback feature in writeFileSync that I am misunderstanding?



Code for reference



outCsv = "x","y","z"
filename = "file.csv"
fs.writeFile(filename, outCsv, function (err) {
if (err) {
return console.log(err);
}
console.log('The file was saved!');
bucket.upload(filename, (err, file) => {
if (err) {
return console.log(err);
}
console.log('The file was uploaded!');
});
});






node.js asynchronous






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 12:41







lukechambers91

















asked Nov 15 '18 at 12:22









lukechambers91lukechambers91

517




517













  • Thanks, jonrsharpe for the edit

    – lukechambers91
    Nov 15 '18 at 12:35



















  • Thanks, jonrsharpe for the edit

    – lukechambers91
    Nov 15 '18 at 12:35

















Thanks, jonrsharpe for the edit

– lukechambers91
Nov 15 '18 at 12:35





Thanks, jonrsharpe for the edit

– lukechambers91
Nov 15 '18 at 12:35












1 Answer
1






active

oldest

votes


















1















Will the next lines of code be halted until the fs.writeFileSync(path, data) is finished?




Yes. It is a blocking operation. Note that you're assuming that fs.writeFileSync does finish.




Are previous asynchronous processes halted by this line of code?




Kinda. Since JavaScript is single-threaded, they will also not be running while the file is writing but will queue up at the next tick of the event loop.




If other async processes are not affected how is writeFileSync different the writeFile?




It blocks any code that comes after it. For an easier example consider the following:



setTimeout(() => console.log('3'), 5);
console.log('1'); // fs.writeFileSync
console.log('2');


vs



setTimeout(() => console.log('3'), 5);
setTimeout(() => console.log('1'), 0); // fs.writeFile
console.log('2');


The first will print 1 2 3 because the call to console.log blocks what comes after. The second will print 2 1 3 because the setTimeout is non-blocking. The code that prints 3 isn't affected either way: 3 will always come last.




Is there a callback feature in writeFileSync that I am misunderstanding?




Don't know. You didn't post enough code for us to say.



This all begs the question of why to prefer fs.writeFile over the alternative. The answer is this:



The sync version blocks.



While it's taking however long it takes your e.g. webserver isn't handling requests.






share|improve this answer





















  • 1





    Thank you so much this was very helpful!

    – lukechambers91
    Nov 15 '18 at 12:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319436%2funderstanding-fs-writefilesyncpath-data-options-node-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















Will the next lines of code be halted until the fs.writeFileSync(path, data) is finished?




Yes. It is a blocking operation. Note that you're assuming that fs.writeFileSync does finish.




Are previous asynchronous processes halted by this line of code?




Kinda. Since JavaScript is single-threaded, they will also not be running while the file is writing but will queue up at the next tick of the event loop.




If other async processes are not affected how is writeFileSync different the writeFile?




It blocks any code that comes after it. For an easier example consider the following:



setTimeout(() => console.log('3'), 5);
console.log('1'); // fs.writeFileSync
console.log('2');


vs



setTimeout(() => console.log('3'), 5);
setTimeout(() => console.log('1'), 0); // fs.writeFile
console.log('2');


The first will print 1 2 3 because the call to console.log blocks what comes after. The second will print 2 1 3 because the setTimeout is non-blocking. The code that prints 3 isn't affected either way: 3 will always come last.




Is there a callback feature in writeFileSync that I am misunderstanding?




Don't know. You didn't post enough code for us to say.



This all begs the question of why to prefer fs.writeFile over the alternative. The answer is this:



The sync version blocks.



While it's taking however long it takes your e.g. webserver isn't handling requests.






share|improve this answer





















  • 1





    Thank you so much this was very helpful!

    – lukechambers91
    Nov 15 '18 at 12:56
















1















Will the next lines of code be halted until the fs.writeFileSync(path, data) is finished?




Yes. It is a blocking operation. Note that you're assuming that fs.writeFileSync does finish.




Are previous asynchronous processes halted by this line of code?




Kinda. Since JavaScript is single-threaded, they will also not be running while the file is writing but will queue up at the next tick of the event loop.




If other async processes are not affected how is writeFileSync different the writeFile?




It blocks any code that comes after it. For an easier example consider the following:



setTimeout(() => console.log('3'), 5);
console.log('1'); // fs.writeFileSync
console.log('2');


vs



setTimeout(() => console.log('3'), 5);
setTimeout(() => console.log('1'), 0); // fs.writeFile
console.log('2');


The first will print 1 2 3 because the call to console.log blocks what comes after. The second will print 2 1 3 because the setTimeout is non-blocking. The code that prints 3 isn't affected either way: 3 will always come last.




Is there a callback feature in writeFileSync that I am misunderstanding?




Don't know. You didn't post enough code for us to say.



This all begs the question of why to prefer fs.writeFile over the alternative. The answer is this:



The sync version blocks.



While it's taking however long it takes your e.g. webserver isn't handling requests.






share|improve this answer





















  • 1





    Thank you so much this was very helpful!

    – lukechambers91
    Nov 15 '18 at 12:56














1












1








1








Will the next lines of code be halted until the fs.writeFileSync(path, data) is finished?




Yes. It is a blocking operation. Note that you're assuming that fs.writeFileSync does finish.




Are previous asynchronous processes halted by this line of code?




Kinda. Since JavaScript is single-threaded, they will also not be running while the file is writing but will queue up at the next tick of the event loop.




If other async processes are not affected how is writeFileSync different the writeFile?




It blocks any code that comes after it. For an easier example consider the following:



setTimeout(() => console.log('3'), 5);
console.log('1'); // fs.writeFileSync
console.log('2');


vs



setTimeout(() => console.log('3'), 5);
setTimeout(() => console.log('1'), 0); // fs.writeFile
console.log('2');


The first will print 1 2 3 because the call to console.log blocks what comes after. The second will print 2 1 3 because the setTimeout is non-blocking. The code that prints 3 isn't affected either way: 3 will always come last.




Is there a callback feature in writeFileSync that I am misunderstanding?




Don't know. You didn't post enough code for us to say.



This all begs the question of why to prefer fs.writeFile over the alternative. The answer is this:



The sync version blocks.



While it's taking however long it takes your e.g. webserver isn't handling requests.






share|improve this answer
















Will the next lines of code be halted until the fs.writeFileSync(path, data) is finished?




Yes. It is a blocking operation. Note that you're assuming that fs.writeFileSync does finish.




Are previous asynchronous processes halted by this line of code?




Kinda. Since JavaScript is single-threaded, they will also not be running while the file is writing but will queue up at the next tick of the event loop.




If other async processes are not affected how is writeFileSync different the writeFile?




It blocks any code that comes after it. For an easier example consider the following:



setTimeout(() => console.log('3'), 5);
console.log('1'); // fs.writeFileSync
console.log('2');


vs



setTimeout(() => console.log('3'), 5);
setTimeout(() => console.log('1'), 0); // fs.writeFile
console.log('2');


The first will print 1 2 3 because the call to console.log blocks what comes after. The second will print 2 1 3 because the setTimeout is non-blocking. The code that prints 3 isn't affected either way: 3 will always come last.




Is there a callback feature in writeFileSync that I am misunderstanding?




Don't know. You didn't post enough code for us to say.



This all begs the question of why to prefer fs.writeFile over the alternative. The answer is this:



The sync version blocks.



While it's taking however long it takes your e.g. webserver isn't handling requests.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 14:33

























answered Nov 15 '18 at 12:53









Jared SmithJared Smith

9,82932043




9,82932043








  • 1





    Thank you so much this was very helpful!

    – lukechambers91
    Nov 15 '18 at 12:56














  • 1





    Thank you so much this was very helpful!

    – lukechambers91
    Nov 15 '18 at 12:56








1




1





Thank you so much this was very helpful!

– lukechambers91
Nov 15 '18 at 12:56





Thank you so much this was very helpful!

– lukechambers91
Nov 15 '18 at 12:56




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319436%2funderstanding-fs-writefilesyncpath-data-options-node-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly