@readytogo/file v1.0.0
readytogo-file
Safely perform async operations on the same file
Usage
import { getFile } from '@readytogo/file';
const file = getFile('./data.txt');
file.read().then((data) => {
console.log(data);
});
const newData = "new Data";
file.write(newData).then(() => {
console.log("success");
});
Note
If you used fs/promises directly, then the usage above would result in both read and write operations happening concurrently (asynchronously in small blocks). It would read a small block, write a small block, read, write, and result in unpredictable behavior. The write operation could complete before the read operation, resulting in a loss of data. For this reason, this module gives each file an asynchronous queue to process read/write operations in the order they are called, without blocking the event loop. This would make the usage above read the file completely first, then write the file completely. This still allows concurrent reading/writing of DIFFERENT files because there would be no race conditions.
1 year ago