Can multiple fs.write be added to a single file to guarantee the execution order?

Suppose we have a program like this:

// imagine the string1 to string1000 are very long strings, which will take a while to be written to file system var arr = ["string1",...,"string1000"]; for (let i = 1; i < 1000; i++) { fs.write("./same/path/file.txt", arr[i], {flag: "a"}}); } 

My question is: will string1 to string1000 be gurantted to append to the same file in order?

Since fs.write is an asynchronous function, I'm not sure how each fs.write () call is made. I assume that the function call for each line should be placed somewhere in another thread (e.g. callstack ?), And as soon as the previous call is made, the next call can be made.

I am not sure, as far as I understand.

Change 1

As in the comments and answers, I see that fs.write not safe to write to the same file multiple fs.write without waiting for a callback . But what about writestream?

If I use the following code, would I guarantee the spelling order?

 // imagine the string1 to string1000 are very long strings, which will take a while to be written to file system var arr = ["string1",...,"string1000"]; var fileStream = fs.createWriteFileStream("./same/path/file.txt", { "flags": "a+" }); for (let i = 1; i < 1000; i++) { fileStream.write(arr[i]); } fileStream.on("error", () => {// do something}); fileStream.on("finish", () => {// do something}); fileStream.end(); 

Any comments or corrections will be helpful! Thanks!

+6
source share
2 answers

docs say that

Note that it is not safe to use fs.write several times in the same file without waiting for a callback. It is strongly recommended that you use fs.createWriteStream for this scenario.

Using a stream works because streams essentially guarantee that the order of the lines written on them is the same order that is read from them.

 var stream = fs.createWriteStream("./same/path/file.txt"); stream.on('error', console.error); arr.forEach((str) => { stream.write(str + '\n'); }); stream.end(); 

Another way to use fs.write , but also to make sure everything is okay, is to use promises to support consistent logic.

 function writeToFilePromise(str) { return new Promise((resolve, reject) => { fs.write("./same/path/file.txt", str, {flag: "a"}}, (err) => { if (err) return reject(err); resolve(); }); }); } // for every string, // write it to the file, // then write the next one once that one is finished and so on arr.reduce((chain, str) => { return chain .then(() => writeToFilePromise(str)); }, Promise.resolve()); 
+4
source

You can synchronize file access using read / write lock for node, see the following example: you can read the documentation

 var ReadWriteLock = require('rwlock'); var lock = new ReadWriteLock(); lock.writeLock(function (release) { fs.appendFile(fileName, addToFile, function(err, data) { if(err) console.log("write error); else console.log("write ok"); release(); // unlock }); }); 
0
source

Source: https://habr.com/ru/post/1011785/


All Articles