Run a function for every line in a file following its output. This is just a more modern version of the code found at https://stackoverflow.com/questions/11225001/reading-a-file-in-real-time-using-node-js and only uses open from fs/promises.
Just pass a callback that consumes the lines of the followed file.
import tail from './tail.js';
tail('test.txt', line => {
console.log(`reader 1: "${line}"`);
});
const done2 = tail('test.txt', { sleepTimeout: 500 }, line => {
console.log(`reader 2: "${line}"`);
});
// Second reader terminates after 5s
setTimeout(() => {
console.log('Stopping 2');
done2();
}, 5000);(this example can be tryied out with the example-writer.js file below)