【发布时间】: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