【问题标题】:Why is Node RxJS .subscribe() not being triggered with bindNodeCallback()?为什么 Node RxJS .subscribe() 没有被 bindNodeCallback() 触发?
【发布时间】:2019-08-24 11:18:12
【问题描述】:

我正在学习如何将 RxJS 与 Node.js 一起使用。查看示例(12 (old)3),到目前为止,我无法触发 .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);

我正在使用"rxjs": "^6.4.0",节点 v10.14.2。

如何更改此代码,以便我的观察者观察配置文件加载?

【问题讨论】:

  • 发布问题本身有时有助于回答问题。如果我使用fs.readFile 而不是fs.readFileSync,一切正常。请帮我解释为什么会这样作为答案?
  • 你可能会觉得这篇文章很有趣medium.freecodecamp.org/rxjs-and-node-8f4e0acebc7c

标签: node.js rxjs observers


【解决方案1】:

您应该使用 readFile 而不是 readFileSync,因为 readFileSync 不适用于回调。

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

【讨论】:

    猜你喜欢
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多