Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I suggest the order of RxJs forkJoin execution on the server?

Tags:

angular

rxjs

In the following forkJoin code, I want requestTwo to execute only after requestOne has executed. Can this be done? From my tests, requestTwo completes before requestOne. Or is their an RxJs function that would ensure the order of execution?

forkJoin({
  requestOne: this.fillOrderService.changeOrderStatus({
    orderNo: this.orderNo,
    orderStatus: this.orderStatus,
    paymentIntent: paymentIntent,
    amount: this.processingOrder?.subtotal
  }),
  requestTwo: this.fillOrderService.getOrders('pending'),
}).subscribe(({ requestOne, requestTwo }) => {
  this.orders = requestTwo;
  this.orders = this.orders.filter(order => order._id !== this.processingOrder._id)

  this.processingOrder = requestOne;
  this.orderNo = this.processingOrder?._id;
  this.orderStatus = this.processingOrder?.status;
});
like image 389
koque Avatar asked Jan 26 '26 09:01

koque


1 Answers

combineLatest executes all sources at the same time.

To fire your requests sequentially, you can chain them together using pipe(switchMap()).

result$ = this.fillOrderService.changeOrderStatus(...).pipe(
    switchMap(requestOne => this.fillOrderService.getOrders('pending').pipe(
        map(requestTwo => ({ requestOne, requestTwo })
    ))
);

Here switchMap internally subscribes to the "inner" observable (getOrders()) and emits its results. We can use map to transform the emission from the second request into an object that contains both response objects.

like image 61
BizzyBob Avatar answered Jan 29 '26 00:01

BizzyBob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!