[英]timeout() operator not working in pipe Rxjs subject
I have an Issue with timeout operator in subject.我在主题中有超时运算符的问题。
my problem look like sample above, but I got all of source,我的问题看起来像上面的示例,但我得到了所有的来源,
response: observable: 1 observable: 2响应:可观察:1 可观察:2
url: https://stackblitz.com/edit/ou5yp1?file=index.ts url: https://stackblitz.com/edit/ou5yp1?file=index.ts
import { Subject, from,Observable } from 'rxjs';
import {timeout} from 'rxjs/operators';
const subject = new Subject();
subject.subscribe({
next: (v) => console.log(`observer: ${v}`),
error: (e) => console.log(`There is an Error ${e}`)
});
subject.pipe(timeout(2000));
const observable = new Observable( sub=> {
sub.next( 1);
setTimeout(()=> {
sub.next(2)
},5000)
})
observable.subscribe(subject);
You have subscribed to wrong observable.你订阅了错误的 observable。
subject.pipe(timeout(2000));
The above line does not apply to subject itself, but instead returns a new observable which has 2 seconds timeout.上述行不适用于主题本身,而是返回一个具有 2 秒超时的新 observable。 So you should subscribe to this returned observable instead of subject itself.所以你应该订阅这个返回的 observable 而不是主题本身。 So your code should be:所以你的代码应该是:
subject.pipe(timeout(2000)).subscribe({
next: (v) => console.log(`observer: ${v}`),
error: (e) => console.log(`There is an Error ${e}`)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.