Return an empty Observable

前端 未结 11 1029
北荒
北荒 2020-11-30 18:23

The function more() is supposed to return an Observable from a get request

export class Collection{

    public more = (): Observab         


        
相关标签:
11条回答
  • 2020-11-30 19:02

    Try this

    export class Collection{
    public more (): Observable<Response> {
       if (this.hasMore()) {
         return this.fetch();
       }
       else{
         return this.returnEmpty(); 
       }            
      }
    public returnEmpty(): any {
        let subscription = source.subscribe(
          function (x) {
           console.log('Next: %s', x);
        },
        function (err) {
           console.log('Error: %s', err);
        },
        function () {
           console.log('Completed');
        });
        }
      }
    let source = Observable.empty();
    
    0 讨论(0)
  • 2020-11-30 19:03

    You can return Observable.of(empty_variable), for example

    Observable.of('');
    
    // or
    Observable.of({});
    
    // etc
    
    0 讨论(0)
  • 2020-11-30 19:06

    For typescript you can specify generic param of your empty observable like this:

    import 'rxjs/add/observable/empty' 
    
    Observable.empty<Response>();
    
    0 讨论(0)
  • 2020-11-30 19:07

    RxJS6 (without compatibility package installed)

    There's now an EMPTY constant and an empty function.

      import { Observable, empty, of } from 'rxjs';
    
      var delay = empty().pipe(delay(1000));     
      var delay2 = EMPTY.pipe(delay(1000));
    

    Observable.empty() doesn't exist anymore.

    0 讨论(0)
  • Yes, there is am Empty operator

    Rx.Observable.empty();
    

    For typescript, you can use from:

    Rx.Observable<Response>.from([])
    
    0 讨论(0)
提交回复
热议问题