0

I'm learning how to use RxJS with Node. Looking at examples (1, 2 (old), 3), I'm so far unable to trigger the 'results' or 'err' methods in .subscribe():

const fs = require('fs');
const { bindNodeCallback } = require('rxjs');

const configPath = './config/config.yml';
const configEncoding = 'utf8';

// also tried with bindCallback()...
const readFileSync = bindNodeCallback(fs.readFileSync);

readFileSync(configPath, configEncoding)
.subscribe(
  results => console.log(results), 
  err => console.error(err)
);

// temporary, exits immediately otherwise
// hmmm clue to the above not being correct?...
setInterval(() => { let keep = 'going'; }, 1000);

I'm using "rxjs": "^6.4.0", node v10.14.2.

How may I alter this code so that my observer observes the config file loading?

Christopher Stevens
  • 1,214
  • 17
  • 32
  • Posting questions in itself sometimes helps answer a question. If I use `fs.readFile` instead of `fs.readFileSync`, all works well. Please help me explain why this is the case as an answer? – Christopher Stevens Apr 03 '19 at 03:19
  • You may find this article interesting https://medium.freecodecamp.org/rxjs-and-node-8f4e0acebc7c – Picci Apr 03 '19 at 05:33

1 Answers1

1

you should use readFile instead of readFileSync besause readFileSync does not work with a callback.

https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options

guuus
  • 26
  • 3