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);
}
}