问题
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')
)
Prints
"created received 42"
"ofd received 42"
"created threw TimeoutError"
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??
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)
回答1:
Observable.of
is completing the stream right after the value has been emitted.
Observable.create
keeps the observable opened. And that's why the timeout
is throwing an error.
Replace
const created = Rx.Observable.create(observer => {
observer.next(42)
})
By
const created = Rx.Observable.create(observer => {
observer.next(42);
observer.complete();
})
and there's no error anymore.
来源:https://stackoverflow.com/questions/46850852/rxjs-created-observable-timeout-always-errors