1

I have those 4 api calls

1. HttpService.PostAsync<any, any>(`post_update_ac_translates`)
2. HttpService.PostAsync<any, any>(`post_start_auto_ac_mapping`)
3. HttpService.PostAsync<any, any>(`get_std_leaf_ac_items`)
4. HttpService.PostAsync<any, any>(`get_all_ac_translates`)

I want to execute them in this order

first execute 1, when completed 2, then after 2 is completed, 3 and 4 in the same time.

I tried to do this like this :

concat(
    HttpService.PostAsync<any, any>(`post_update_ac_translates`, {p_id: this.props.projectId, ac_trans: data}, HttpService.AuditcoreAPIBasePath),
    HttpService.PostAsync<any, any>(`post_start_auto_ac_mapping`, {p_id: this.props.projectId}, HttpService.AuditcoreAPIBasePath)
).subscribe(() => {
    forkJoin(
        HttpService.GetAsync<any, any>(`get_std_leaf_ac_items`, {p_id: this.props.projectId}, HttpService.AuditcoreAPIBasePath),
        HttpService.GetAsync<any, any>(`get_all_ac_translates`, {p_id: this.props.projectId}, HttpService.AuditcoreAPIBasePath)
    ).subscribe(([resp1, resp2]) => {

But the code is executing has follow

1 => 3/4 => 2 => 3/4

instead of

1=> 2 => 3/4

doing

HttpService.PostAsync<any, any>(`post_update_ac_translates`, {p_id: this.props.projectId, ac_trans: data}, HttpService.AuditcoreAPIBasePath).concat(

return an error

Property 'concat' does not exist on type 'AxiosObservable'.ts(2339)

1 Answer 1

2

Instead of nesting the subscribe() calls, I would suggest the following method of chaining RxJS pipeable operators, as it might look cleaner.

HttpService.PostAsync<any, any>(`post_update_ac_translates`)
  .pipe(
    switchMap(() => HttpService.PostAsync<any, any>(`post_start_auto_ac_mapping`))
    switchMap(() => forkJoin(HttpService.PostAsync<any, any>(`get_std_leaf_ac_items`), HttpService.PostAsync<any, any>(`get_all_ac_translates`))
  ).subscribe([res1, res2] => {
     console.log(res1);
     // do the rest
  });

First, we use switchMap() to map the observable values from the first HTTP request into an inner observable, followed by making the second HTTP request. Then, we chain another switchMap() operator which will carry out the forkJoin operation which waits for the third and forth HTTP requests to be completed, before we subscribe and return the values from the observable.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks that looks great, I just would like to know if it's possible to get the response from 3/4 (so your last switchmap) and execute code using those resp ? is it going to be subscribe([resp1,resp2,resp3,resp4])
I tried subscribe(([resp1, resp2, [resp3, resp4]]) but I got > Tuple type '[AxiosResponse<any>, AxiosResponse<any>]' of length '2' has no element at index '2'
@Bobby yes you can! Check my edited answer. By doing so, you will be able to get the responses from the 3rd and 4th HTTP requests

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.