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;
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With