RXJS while loop for pagination

前端 未结 2 1112
花落未央
花落未央 2021-02-08 12:56

I try to query all people data from the swap api. The URL swapi.co/api/people returns an object with an people array and the URL(swapi.co/api/people/?page=2) where I get the nex

2条回答
  •  没有蜡笔的小新
    2021-02-08 13:55

    Use expand operator like:

    function getData(id) {
       let next = id<5 ? id+1 : null;
       let obj = {id:id, next:next}
       return next ?   Rx.Observable.of(obj) : Rx.Observable.empty();
     }
    
     getData(0)
       .expand(el=> getData(el.next))
       .subscribe(x=>console.log(x));
    

提交回复
热议问题