创建可观察的超时总是错误

创建可观察的超时总是错误

本文介绍了rxjs 创建可观察的超时总是错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

好的,所以现在我真的很困惑.执行以下代码

ok, so now I'm really puzzled. Executing the following code

const created = Rx.Observable.create(observer => {
  observer.next(42)
})
const ofd = Rx.Observable.of(42)

const createSub = name => [
  val => console.log(`${name} received ${val}`),
  error => console.log(`${name} threw ${error.constructor.name}`)
]

created
  .timeout(100)
  .subscribe(
    ...createSub('created')
  )

ofd
  .timeout(100)
  .subscribe(
    ...createSub('ofd')
  )

印刷品

"created received 42"
"ofd received 42"
"created threw TimeoutError"

我完全不明白,为什么 created Observable 发出一个值但 ofd Observable 却没有??

I don't understand this at all, why does the created Observable error even though it emits a value but the ofd Observable does not??

使用 RxJS 5,jsbin.com 中的 5.0.3 和我的应用中的 5.4.3 出现问题.

Using RxJS 5, problem occurs with 5.0.3 in jsbin.com and 5.4.3 in my app.

(注意:这也发生在主题上,这促使我创建了这个例子)

(Note: This happens with subjects too, which led me to create this example)

推荐答案

Observable.of 在值发出后立即完成流.

Observable.of is completing the stream right after the value has been emitted.

Observable.create 保持 observable 打开.这就是 timeout 抛出错误的原因.

Observable.create keeps the observable opened. And that's why the timeout is throwing an error.

替换

const created = Rx.Observable.create(observer => {
  observer.next(42)
})

通过

const created = Rx.Observable.create(observer => {
  observer.next(42);
  observer.complete();
})

不再有错误.

这篇关于rxjs 创建可观察的超时总是错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:09