Search code examples
node.jsrxjsobservers

Why is Node RxJS .subscribe() not being triggered with bindNodeCallback()?


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?


Solution

  • 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