Await promisified fs.writeFile vs fs.writeFileSync












-2















Are there some advantages of one of this options?



1.



const fs = require('fs')

const testFunc1 = async () => {
fs.writeFileSync('text.txt', 'hello world')
}


2.



const fs = require('fs')
const util = require('util')
const writeFilePromisified = util.promisify(fs.writeFile)

const testFunc2 = async () => {
await writeFilePromisified('text.txt', 'hello world')
}


I am aware the difference betweeen writeFile and writeFileSync. The question is are there some diffs between promisses that return testFunc1 and testFunc2. So ist it the same to calling
testFunc1.then(...) // or await testFunc1
or
testFunc2.then(...) // or await testFunc2



These both promisses will be fullfilled when file writing is done.










share|improve this question





























    -2















    Are there some advantages of one of this options?



    1.



    const fs = require('fs')

    const testFunc1 = async () => {
    fs.writeFileSync('text.txt', 'hello world')
    }


    2.



    const fs = require('fs')
    const util = require('util')
    const writeFilePromisified = util.promisify(fs.writeFile)

    const testFunc2 = async () => {
    await writeFilePromisified('text.txt', 'hello world')
    }


    I am aware the difference betweeen writeFile and writeFileSync. The question is are there some diffs between promisses that return testFunc1 and testFunc2. So ist it the same to calling
    testFunc1.then(...) // or await testFunc1
    or
    testFunc2.then(...) // or await testFunc2



    These both promisses will be fullfilled when file writing is done.










    share|improve this question



























      -2












      -2








      -2








      Are there some advantages of one of this options?



      1.



      const fs = require('fs')

      const testFunc1 = async () => {
      fs.writeFileSync('text.txt', 'hello world')
      }


      2.



      const fs = require('fs')
      const util = require('util')
      const writeFilePromisified = util.promisify(fs.writeFile)

      const testFunc2 = async () => {
      await writeFilePromisified('text.txt', 'hello world')
      }


      I am aware the difference betweeen writeFile and writeFileSync. The question is are there some diffs between promisses that return testFunc1 and testFunc2. So ist it the same to calling
      testFunc1.then(...) // or await testFunc1
      or
      testFunc2.then(...) // or await testFunc2



      These both promisses will be fullfilled when file writing is done.










      share|improve this question
















      Are there some advantages of one of this options?



      1.



      const fs = require('fs')

      const testFunc1 = async () => {
      fs.writeFileSync('text.txt', 'hello world')
      }


      2.



      const fs = require('fs')
      const util = require('util')
      const writeFilePromisified = util.promisify(fs.writeFile)

      const testFunc2 = async () => {
      await writeFilePromisified('text.txt', 'hello world')
      }


      I am aware the difference betweeen writeFile and writeFileSync. The question is are there some diffs between promisses that return testFunc1 and testFunc2. So ist it the same to calling
      testFunc1.then(...) // or await testFunc1
      or
      testFunc2.then(...) // or await testFunc2



      These both promisses will be fullfilled when file writing is done.







      node.js promise






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 15:32







      shurik

















      asked Nov 16 '18 at 12:11









      shurikshurik

      83




      83
























          3 Answers
          3






          active

          oldest

          votes


















          1














          fs already contains promisified API that doesn't need promisify.



          Asynchronous promise-based version requires to use it as a part of promise-based control flow, while synchronous version doesn't impose this requirement.



          Asynchronous readFile/writeFile is non-blocking, while synchronous readFileSync/writeFileSync is blocking but allows to complete a job faster. This may be noticeable during intensive IO operations.






          share|improve this answer



















          • 1





            fs.promise API is experimental as for node 10.

            – shurik
            Nov 16 '18 at 15:14











          • Colud you please explain difference between two Promisses that return testFunc1 and testFunc2. As i can see there is no diffs there.

            – shurik
            Nov 16 '18 at 15:22






          • 1





            testFunc1() is useless. It doesn't benefit from being a promise. It blocks main thread because writeFileSync is synchronous. And then provides a tiny delay if resulting promise is awaited.

            – estus
            Nov 16 '18 at 15:40





















          0














          fs.readFile takes a callback function, which means it will not block the execution of your script.
          fs.readFileSync however does not take a callback, which means that the execution of your script will be paused untill the process is finished.
          Using promisfy is one way to solve this issue, for small files it wont make a difference, but for large files you might want to transform fs.readFileSync into a promise so you wont block the execution.
          Hope that helps.






          share|improve this answer































            0














            To illustrate the difference between two promises that returns functions:



            const fs = require('fs')
            const util = require('util')

            const testFunc1 = async () => {
            fs.writeFileSync('text.txt', 'hello world')
            console.log('file write done with writeFileSync')
            }

            const writeFilePromisified = util.promisify(fs.writeFile)

            const testFunc2 = async () => {
            await writeFilePromisified('text.txt', 'hello world')
            console.log('file write done with promisified writeFile')
            }

            console.log('start test1')
            testFunc1().then(() => {
            console.log('promise 1 is fullfiled')
            })
            console.log('start test2')
            testFunc2().then(() => {
            console.log('promise 2 is fullfiled')
            })
            console.log('stop')


            Output would be:



            start test1
            file write done with writeFileSync
            start test2
            stop
            promise 1 is fullfiled
            file write done with promisified writeFile
            promise 2 is fullfiled


            So like estus said testFunc1 blocks the execution of the main thread. testFunc2 do not block.






            share|improve this answer























              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%2f53337663%2fawait-promisified-fs-writefile-vs-fs-writefilesync%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              fs already contains promisified API that doesn't need promisify.



              Asynchronous promise-based version requires to use it as a part of promise-based control flow, while synchronous version doesn't impose this requirement.



              Asynchronous readFile/writeFile is non-blocking, while synchronous readFileSync/writeFileSync is blocking but allows to complete a job faster. This may be noticeable during intensive IO operations.






              share|improve this answer



















              • 1





                fs.promise API is experimental as for node 10.

                – shurik
                Nov 16 '18 at 15:14











              • Colud you please explain difference between two Promisses that return testFunc1 and testFunc2. As i can see there is no diffs there.

                – shurik
                Nov 16 '18 at 15:22






              • 1





                testFunc1() is useless. It doesn't benefit from being a promise. It blocks main thread because writeFileSync is synchronous. And then provides a tiny delay if resulting promise is awaited.

                – estus
                Nov 16 '18 at 15:40


















              1














              fs already contains promisified API that doesn't need promisify.



              Asynchronous promise-based version requires to use it as a part of promise-based control flow, while synchronous version doesn't impose this requirement.



              Asynchronous readFile/writeFile is non-blocking, while synchronous readFileSync/writeFileSync is blocking but allows to complete a job faster. This may be noticeable during intensive IO operations.






              share|improve this answer



















              • 1





                fs.promise API is experimental as for node 10.

                – shurik
                Nov 16 '18 at 15:14











              • Colud you please explain difference between two Promisses that return testFunc1 and testFunc2. As i can see there is no diffs there.

                – shurik
                Nov 16 '18 at 15:22






              • 1





                testFunc1() is useless. It doesn't benefit from being a promise. It blocks main thread because writeFileSync is synchronous. And then provides a tiny delay if resulting promise is awaited.

                – estus
                Nov 16 '18 at 15:40
















              1












              1








              1







              fs already contains promisified API that doesn't need promisify.



              Asynchronous promise-based version requires to use it as a part of promise-based control flow, while synchronous version doesn't impose this requirement.



              Asynchronous readFile/writeFile is non-blocking, while synchronous readFileSync/writeFileSync is blocking but allows to complete a job faster. This may be noticeable during intensive IO operations.






              share|improve this answer













              fs already contains promisified API that doesn't need promisify.



              Asynchronous promise-based version requires to use it as a part of promise-based control flow, while synchronous version doesn't impose this requirement.



              Asynchronous readFile/writeFile is non-blocking, while synchronous readFileSync/writeFileSync is blocking but allows to complete a job faster. This may be noticeable during intensive IO operations.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 16 '18 at 12:23









              estusestus

              68.6k21101215




              68.6k21101215








              • 1





                fs.promise API is experimental as for node 10.

                – shurik
                Nov 16 '18 at 15:14











              • Colud you please explain difference between two Promisses that return testFunc1 and testFunc2. As i can see there is no diffs there.

                – shurik
                Nov 16 '18 at 15:22






              • 1





                testFunc1() is useless. It doesn't benefit from being a promise. It blocks main thread because writeFileSync is synchronous. And then provides a tiny delay if resulting promise is awaited.

                – estus
                Nov 16 '18 at 15:40
















              • 1





                fs.promise API is experimental as for node 10.

                – shurik
                Nov 16 '18 at 15:14











              • Colud you please explain difference between two Promisses that return testFunc1 and testFunc2. As i can see there is no diffs there.

                – shurik
                Nov 16 '18 at 15:22






              • 1





                testFunc1() is useless. It doesn't benefit from being a promise. It blocks main thread because writeFileSync is synchronous. And then provides a tiny delay if resulting promise is awaited.

                – estus
                Nov 16 '18 at 15:40










              1




              1





              fs.promise API is experimental as for node 10.

              – shurik
              Nov 16 '18 at 15:14





              fs.promise API is experimental as for node 10.

              – shurik
              Nov 16 '18 at 15:14













              Colud you please explain difference between two Promisses that return testFunc1 and testFunc2. As i can see there is no diffs there.

              – shurik
              Nov 16 '18 at 15:22





              Colud you please explain difference between two Promisses that return testFunc1 and testFunc2. As i can see there is no diffs there.

              – shurik
              Nov 16 '18 at 15:22




              1




              1





              testFunc1() is useless. It doesn't benefit from being a promise. It blocks main thread because writeFileSync is synchronous. And then provides a tiny delay if resulting promise is awaited.

              – estus
              Nov 16 '18 at 15:40







              testFunc1() is useless. It doesn't benefit from being a promise. It blocks main thread because writeFileSync is synchronous. And then provides a tiny delay if resulting promise is awaited.

              – estus
              Nov 16 '18 at 15:40















              0














              fs.readFile takes a callback function, which means it will not block the execution of your script.
              fs.readFileSync however does not take a callback, which means that the execution of your script will be paused untill the process is finished.
              Using promisfy is one way to solve this issue, for small files it wont make a difference, but for large files you might want to transform fs.readFileSync into a promise so you wont block the execution.
              Hope that helps.






              share|improve this answer




























                0














                fs.readFile takes a callback function, which means it will not block the execution of your script.
                fs.readFileSync however does not take a callback, which means that the execution of your script will be paused untill the process is finished.
                Using promisfy is one way to solve this issue, for small files it wont make a difference, but for large files you might want to transform fs.readFileSync into a promise so you wont block the execution.
                Hope that helps.






                share|improve this answer


























                  0












                  0








                  0







                  fs.readFile takes a callback function, which means it will not block the execution of your script.
                  fs.readFileSync however does not take a callback, which means that the execution of your script will be paused untill the process is finished.
                  Using promisfy is one way to solve this issue, for small files it wont make a difference, but for large files you might want to transform fs.readFileSync into a promise so you wont block the execution.
                  Hope that helps.






                  share|improve this answer













                  fs.readFile takes a callback function, which means it will not block the execution of your script.
                  fs.readFileSync however does not take a callback, which means that the execution of your script will be paused untill the process is finished.
                  Using promisfy is one way to solve this issue, for small files it wont make a difference, but for large files you might want to transform fs.readFileSync into a promise so you wont block the execution.
                  Hope that helps.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 12:38









                  squeekyDavesqueekyDave

                  383114




                  383114























                      0














                      To illustrate the difference between two promises that returns functions:



                      const fs = require('fs')
                      const util = require('util')

                      const testFunc1 = async () => {
                      fs.writeFileSync('text.txt', 'hello world')
                      console.log('file write done with writeFileSync')
                      }

                      const writeFilePromisified = util.promisify(fs.writeFile)

                      const testFunc2 = async () => {
                      await writeFilePromisified('text.txt', 'hello world')
                      console.log('file write done with promisified writeFile')
                      }

                      console.log('start test1')
                      testFunc1().then(() => {
                      console.log('promise 1 is fullfiled')
                      })
                      console.log('start test2')
                      testFunc2().then(() => {
                      console.log('promise 2 is fullfiled')
                      })
                      console.log('stop')


                      Output would be:



                      start test1
                      file write done with writeFileSync
                      start test2
                      stop
                      promise 1 is fullfiled
                      file write done with promisified writeFile
                      promise 2 is fullfiled


                      So like estus said testFunc1 blocks the execution of the main thread. testFunc2 do not block.






                      share|improve this answer




























                        0














                        To illustrate the difference between two promises that returns functions:



                        const fs = require('fs')
                        const util = require('util')

                        const testFunc1 = async () => {
                        fs.writeFileSync('text.txt', 'hello world')
                        console.log('file write done with writeFileSync')
                        }

                        const writeFilePromisified = util.promisify(fs.writeFile)

                        const testFunc2 = async () => {
                        await writeFilePromisified('text.txt', 'hello world')
                        console.log('file write done with promisified writeFile')
                        }

                        console.log('start test1')
                        testFunc1().then(() => {
                        console.log('promise 1 is fullfiled')
                        })
                        console.log('start test2')
                        testFunc2().then(() => {
                        console.log('promise 2 is fullfiled')
                        })
                        console.log('stop')


                        Output would be:



                        start test1
                        file write done with writeFileSync
                        start test2
                        stop
                        promise 1 is fullfiled
                        file write done with promisified writeFile
                        promise 2 is fullfiled


                        So like estus said testFunc1 blocks the execution of the main thread. testFunc2 do not block.






                        share|improve this answer


























                          0












                          0








                          0







                          To illustrate the difference between two promises that returns functions:



                          const fs = require('fs')
                          const util = require('util')

                          const testFunc1 = async () => {
                          fs.writeFileSync('text.txt', 'hello world')
                          console.log('file write done with writeFileSync')
                          }

                          const writeFilePromisified = util.promisify(fs.writeFile)

                          const testFunc2 = async () => {
                          await writeFilePromisified('text.txt', 'hello world')
                          console.log('file write done with promisified writeFile')
                          }

                          console.log('start test1')
                          testFunc1().then(() => {
                          console.log('promise 1 is fullfiled')
                          })
                          console.log('start test2')
                          testFunc2().then(() => {
                          console.log('promise 2 is fullfiled')
                          })
                          console.log('stop')


                          Output would be:



                          start test1
                          file write done with writeFileSync
                          start test2
                          stop
                          promise 1 is fullfiled
                          file write done with promisified writeFile
                          promise 2 is fullfiled


                          So like estus said testFunc1 blocks the execution of the main thread. testFunc2 do not block.






                          share|improve this answer













                          To illustrate the difference between two promises that returns functions:



                          const fs = require('fs')
                          const util = require('util')

                          const testFunc1 = async () => {
                          fs.writeFileSync('text.txt', 'hello world')
                          console.log('file write done with writeFileSync')
                          }

                          const writeFilePromisified = util.promisify(fs.writeFile)

                          const testFunc2 = async () => {
                          await writeFilePromisified('text.txt', 'hello world')
                          console.log('file write done with promisified writeFile')
                          }

                          console.log('start test1')
                          testFunc1().then(() => {
                          console.log('promise 1 is fullfiled')
                          })
                          console.log('start test2')
                          testFunc2().then(() => {
                          console.log('promise 2 is fullfiled')
                          })
                          console.log('stop')


                          Output would be:



                          start test1
                          file write done with writeFileSync
                          start test2
                          stop
                          promise 1 is fullfiled
                          file write done with promisified writeFile
                          promise 2 is fullfiled


                          So like estus said testFunc1 blocks the execution of the main thread. testFunc2 do not block.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 16:10









                          shurikshurik

                          83




                          83






























                              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%2f53337663%2fawait-promisified-fs-writefile-vs-fs-writefilesync%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

                              How to pass form data using jquery Ajax to insert data in database?

                              National Museum of Racing and Hall of Fame

                              Guess what letter conforming each word