forkjoin是否以特定顺序返回结果?

时间:2017-11-14 09:49:46

标签: angular rxjs

我的应用会进行多个并行的http呼叫。它基于switch-fall虽然逻辑,因此查询的数量会有所不同(1,2或3个调用)。有没有办法让我知道哪个电话先退回?即,是根据列表中的http请求分配密钥还是以有序方式返回?

无论哪种方式都适合我。我只需找到一种方法,将正确的结果与正确的呼叫相匹配。

3 个答案:

答案 0 :(得分:3)

是的,它会按照您在数组中发出请求的顺序返回结果

例如,

const bothrequests= Observable.combineLatest(
  this.http.get('https://testdb1.com/.json').map((res: Response) => res.json()),
  this.http.get('https://testdb2.com/.json').map((res: Response) => res.json())
)
bothrequests.subscribe(latestValues => {

});

答案 1 :(得分:0)

根据请求的顺序进行排序。

答案 2 :(得分:0)

尝试

const { merge, of } = rxjs;
const { bufferCount, take, delay } = rxjs.operators;

let r1 = of("A").pipe(delay(1000));
let r2 = of("C").pipe(delay(3000));
let r3 = of("D").pipe(delay(4000));
let r4 = of("B").pipe(delay(2000));

// in forkJoin(r1,r2,r3,r4) we get:      [A,C,D,B]
// in this we get sorted by finish time: [A,B,C,D]

merge(r1,r2,r3,r4)
  .pipe(bufferCount(4), take(1))      
  .subscribe(console.log)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.min.js" integrity="sha256-85uCh8dPb35sH3WK435rtYUKALbcvEQFC65eg+raeuc=" crossorigin="anonymous"></script>