How do you return an observable from 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);
}
}
A copy and paste example :
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);
}
}
How do you return an observable from 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);
}
}
A copy and paste example :
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);
}
}
Share
Improve this question
edited Apr 23, 2017 at 15:20
Ced
asked Apr 20, 2017 at 18:41
CedCed
17.5k15 gold badges100 silver badges156 bronze badges
1
-
1
it doesn't feel right to use a
setTimeout
with observable. How aboutdelay
operator ? – maxime1992 Commented Apr 20, 2017 at 18:45
1 Answer
Reset to default 4Something like this (you can't return anything from 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);
}
}