Rxnode — reactive nodejs API
At the beginning of 2020, my career as a frontend developer turned in an unexpected direction for me. I haven’t written a single Angular component in a year. I Replaced Angular with server code and code for the CLI. It was a new and interesting experience for me, but it wasn’t easy to solve my usual tasks without RxJS.
Let’s look at a simple example of reading and writing a file using the native features of nodejs
import { readFile, writeFile } from 'fs';
readFile('src/some-file.js', (error, data) => {
if (error) {
console.error(error);
return;
}
writeFile('src/some-file.js', data, (error) => {
if (error) {
console.error(error);
return;
}
console.log('Success!');
});
});
Code with callbacks after Angular seems complicated. First, I translated callbacks to promises.
import * as fs from 'fs';
import { promisify } from 'util';
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
readFile('src/some-file.js')
.then((data) => writeFile('src/some-file.js', data))
.then(() => {
console.log('Success!');
})
.cacth(console.error);
I was OK with this solution until I needed to implement several tasks’ competitive execution with a limit. RxJS has such an opportunity out of the box. Why would I invent my own algorithm…