如何做序列图,以便每个地图等待rxjs中的前一个?

时间:2018-01-31 09:22:29

标签: javascript angular rxjs

const source = Rx.Observable.from([1, 2, 3, 4, 5]);
const example = source.map(val => { /* asynchronous logic here*/ })
                      .map(val => { /* asynchronous logic here*/ });
                      .map(val => { /* asynchronous logic here*/ });

如何让每个map等待前一个map的异步执行?

1 个答案:

答案 0 :(得分:2)

您应该使用concatMap而不是map并从其回调中返回Observable:

const source = Rx.Observable.from([1, 2, 3, 4, 5]);
const example = source
  .concatMap(val => { /* asynchronous logic here*/ })
  .concatMap(val => { /* asynchronous logic here*/ })
  .concatMap(val => { /* asynchronous logic here*/ });