如何从setTimeout
返回一个观察者?
send(action):Observable<any>{
if(this.readyState === 0 ){
setTimeout(() => this.send(action), this.timeout);
}
else{
// observable is an Rxjs observable....
return this.observable$.take(1);
}
}
复制和粘贴示例:
let observable$ = Rx.Observable.fromArray([1, 2, 3, 4, 5]);
timeout = 40;
// if you switch this to 1 it works..
readyState = 0;
setTimeout( () => readyState = 1, 120);
send().subscribe(c => console.log(c));
function send(action){
if(readyState === 0 ){
setTimeout(() => send(action), timeout);
}
else{
return observable$.take(1);
}
}
答案 0 :(得分:4)
这样的事情(你不能从setTimeout()
返回任何内容):
send(action):Observable<any>{
if(this.readyState === 0 ){
return Observable.timer(this.timeout)
.mergeMap(() => this.send(action))
.take(1);
}
else{
// observable is an Rxjs observable....
return this.observable$.take(1);
}
}