【问题标题】:RxJS forkjoin isn't runningRxJS forkjoin 没有运行
【发布时间】:2017-05-28 20:06:54
【问题描述】:

我正在尝试通过rx-http-request 使用 RxJS 从 NodeJS 服务器查询第三方 API。从长远来看,我想使用 RxJS 来处理一些更有趣的错误情况。但目前我在运行一个相当微不足道的案例时遇到了问题。

案例 1(可行):

third_party.js
import {RxHttpRequest} from 'rx-http-request'
export default ((name) => RxHttpRequest.get(`www.third.party.com/${name}`)

然后,

   import third from './third_party'
   third('bob').subscribe(console.log)

案例2(这不是)

   import third from './third_party'
   import Rx from 'rx'
   Rx.Observable.forkJoin(third('bob')).subscribe(console.log)

案例 1 打印(正确的)第三方响应。案例 2 似乎根本没有运行。当我打印出 console.log(Rx.Observable.forkJoin) 时,它会打印出Function,例如我实际上已经包含了 Rx 的正确部分。

案例 3: 在第三方:

export default ((name) => RxHttpRequest.get(`www.third.party.com/${name}`).map((res)=>console.log(res))

内部console.log 触发,但外部订阅不触发。 为什么会发生这种行为?我怎样才能真正将值发送到外部订阅?

【问题讨论】:

  • third('bob') 会发出 complete 吗? forkJoin 需要内部 observable 在转发之前完成,因为它只从内部 observable 发出最后一条消息。
  • 另外,使用do 表示副作用,例如do(e => console.log(e))
  • 如何检查是否发出了完成?如果不是,我该如何强制它完成发射?
  • 你可以做third('bob').subscribe(e => console.log("next: " + e), err => console.log("error: " + err), () => console.log("complete"))
  • 要强制它完成,你需要决定它什么时候完成。我猜最简单的方法是third('bob').take(1)

标签: javascript node.js rxjs observable reactive-programming


【解决方案1】:

如果您像这样导入rx-http-request

import { RxHttpRequest } from 'rx-http-request';

你可能是这样安装的:

npm install --save rx-http-request

你可能没有注意到这条警告信息:

npm WARN 已弃用 rx-http-request@1.2.0:此包不再维护,已移至 @akanass/rx-http-request。

该版本 (1.2.0) 远远落后于 GitHub 存储库 (2.3.0) 中的版本。

尝试使用@akanass/rx-http-request,而不是:

npm install --save @akanass/rx-http-request

import { RxHR } from '@akanass/rx-http-request';

导出似乎已更改,因此您应该咨询docs

此外,您应该注意rx-http-request,因为它包含rxjs 作为dependency,而不是对等依赖项。这开启了安装多个rxjs 软件包的可能性(取决于版本和安装顺序)。我会鼓励你 raise an issue 将其更改为对等依赖项。

【讨论】:

  • 谢谢!以上实际上并没有解决我的问题本身,而是发现了一个我不知道的不同问题:)
猜你喜欢
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2020-02-26
  • 2021-03-10
  • 2018-10-02
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多