javascript - Return an observable from setTimeout (Rxjs) - Stack Overflow

How do you return an observable from setTimeout ? send(action):Observable<any>{if(this.readyState

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 about delay operator ? – maxime1992 Commented Apr 20, 2017 at 18:45
Add a ment  | 

1 Answer 1

Reset to default 4

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745342453a4623390.html

相关推荐

  • javascript - Return an observable from setTimeout (Rxjs) - Stack Overflow

    How do you return an observable from setTimeout ? send(action):Observable<any>{if(this.readyState

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信