0

I have an event which call 3 http observable. order of these request is important to me. so I have to use concat from rxjs. for example with the first request I got the data and fill my local variable values with that, then I want to check if the result of one of those local variable is true then send next request.

Something like that:

onMyEvent(e) {
    let isRoundingAdditionIsRequiered = false;
    let ishavePeriodicalDiscount = false;

    const api = 'api/Sales/SaleTemplate/' + e;
    const firstApiGet = this.dataSourceService.generateCustomApiDataList(
      'sales',
      'SaleTemplate',
      api
    ).dataList$;
    const secondApi= 'api/Sales/Addition/List?$filter=AdditionCode eq "4"'
    const secondApiGet= this.dataSourceService.generateCustomApiDataList(
      'sales',
      'SaleTemplate',
      api
    ).dataList$;
    const postApi = 'api/Sales/Contract/CalculateAllRowPrices/';
    const calculatePrice = this.dataSourceService.generateCustomApiPost
        ('sales', 'Contract', postApi, this.entity).dataList$;
}

Now by the result of firstApiGet I want to set isRoundingAdditionIsRequiered to true. like this:

firstApiGet.subscribe(data => { if (data.RoundingAdditionIsRequiered) {isRoundingAdditionIsRequiered =true} });

then check it for next request:

if (isRoundingAdditionIsRequiered ) {
    secondApi.subscribe(data => {// codes here ...})
}
// and other codes ...

This is not work sequential so I have to use concat operator.

Something like

concat(firstApiGet,secondApiGet,postApi).subscribe(data=>{//...})

So what is the code?

1
  • You might want to try combineLatest or forkJoin instead of contact, your code should look like this combineLatest([firstApiGet, secondApiGet, postApi]).pipe(....).subscribe({ next: ([first, second, third]) => { if (data.RoundingAdditionIsRequiered) {isRoundingAdditionIsRequiered =true} }) Commented Mar 26, 2021 at 23:39

1 Answer 1

1

You can use switchMap operator.

onMyEvent(e) {
  let isRoundingAdditionIsRequiered = false;
  let ishavePeriodicalDiscount = false;
  const api = 'api/Sales/SaleTemplate/' + e;
  const firstApiGet = this.dataSourceService.generateCustomApiDataList(
       'sales', 'SaleTemplate', api ).dataList$;
  const secondApi= 'api/Sales/Addition/List?$filter=AdditionCode eq "4"'
  const secondApiGet= this.dataSourceService.generateCustomApiDataList(
       'sales', 'SaleTemplate', api ).dataList$;
  const postApi = 'api/Sales/Contract/CalculateAllRowPrices/';
  const calculatePrice= this.dataSourceService.generateCustomApiPost(
       'sales','Contract',postApi,this.entity).dataList$;
  }
  firstApiGet.pipe(
     switchMap( data => {
        isRoundingAdditionIsRequiered = data.RoundingAdditionIsRequiered;
        return isRoundingAdditionIsRequiered ? secondApiGet : of( undefined );
     ),
     switchMap( () => calculatePrice )
  ).subscribe();
}
Sign up to request clarification or add additional context in comments.

6 Comments

wow! I know by switch map we can unsubscribe from first request don't I?, so the order should work. let me try it then vote to your Idea
dear @Raid I want to check if isRoundingAdditionIsRequiered ===true then secondApiGet send. by this even if isRoundingAdditionIsRequiered === false again secondApiGet will send.
What should happen if isRoundingAdditionIsRequiered === false?
only firstApiGet and calculatePrice should be subscribed
it seems a perfect Idea, let me try it. by the way you are awesome ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.