File reading and writing are not the bottleneck of my program. I don't want to use the nonblocking version. Plus, the nonblocking version of the code make the code nested which causes readability problems.
Could you answer my original question? Thanks. On Thu, Oct 24, 2019 at 10:27 AM Rouan van der Ende <ro...@8bo.org> wrote: > > Hi Peng > > Ideally you'd want to avoid using the Sync versions of these functions as > they block the system. Here is an example of how you can write to the file > after a read has completed. By passing a function as a callback to be run > when the function has completed. > > var fs = require("fs"); > > console.log("starting read...") > > fs.readFile("./test.txt", (err,data)=>{ > if (err) {console.log(err); return;} > console.log("read is completed.") > console.log(data.toString()) > > var newdata = "some new data timestamp:"+new Date() > console.log("starting write...") > fs.writeFile("./test.txt", newdata, (err)=>{ > if (err) {console.log(err); return;} > console.log("write success") > }) > }) > > On Wednesday, 23 October 2019 17:45:02 UTC+2, Peng Yu wrote: >> >> I'd like to fs.writeFileSync() to the same file after reading its content by >> fs.readFileSync(). It is not clear to me whether fs.closeSync() (or any >> other file closing function) is needed between writeFileSync() and >> readFileSync() according to the documentation. Would you please let me know? >> Thanks. >> >> https://nodejs.org/api/fs.html > > -- > Job board: http://jobs.nodejs.org/ > New group rules: > https://gist.github.com/othiym23/9886289#file-moderation-policy-md > Old group rules: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > --- > You received this message because you are subscribed to a topic in the Google > Groups "nodejs" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/nodejs/Xr9qZEqNuw8/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > nodejs+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/nodejs/6a6d6486-aa71-402f-b138-a1fa2bf170bc%40googlegroups.com. -- Regards, Peng -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CABrM6w%3DFXORBJFeWatZTOMepCWnU%3DTK__RcFZ8BW5bEYcS15uw%40mail.gmail.com.