0

I have a service which is responsible to send and retrieve data to backed. Operations can be: - fetch data (all the model or part of it) - delete one element - update existing element - create new element

Operations can be triggered by user input. I want to make sure that one operation is done only after all the previous are finished. How can this be done with RxJS?

Similar question here, but in that case new operations are not started after the old ones are finished.

Paolo
  • 2,461
  • 5
  • 31
  • 45
  • Possible duplicate of [RXJS Wait for all observables in an array to complete (or error)](https://stackoverflow.com/questions/41734921/rxjs-wait-for-all-observables-in-an-array-to-complete-or-error) – Chris Yungmann Sep 24 '19 at 21:50
  • I think this logic of ensuring that incoming task are executed one after another in the correct order should be added to your backend not your frontend. – frido Sep 25 '19 at 10:12

2 Answers2

1

This can be achieved using concat, which will generate a new Observable. Each time a new operation is requested, the service will have to create a new observable.

https://www.learnrxjs.io/operators/combination/concat.html

const { interval, concat} = rxjs;
const { take } = rxjs.operators;
 
let x = concat(
  interval(500).pipe(take(1)),
  interval(500).pipe(take(2)),
)
 
concat (
  x,
  interval(500).pipe(take(3)),
).subscribe(console.log)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.min.js"></script>
<button>Click</button>
Paolo
  • 2,461
  • 5
  • 31
  • 45
-1

You probably want the forkJoin function, which waits for all passed Observables to complete before emitting.

Chris Yungmann
  • 1,079
  • 6
  • 14