1

I need to subscribe to two http calls, and make sure don't fail after that , I want to apply a filter on the arrays that I get in const array.

  const result =[]
    this.fncservice.obs.subscribe((response)=>this.fncservice.maptofncbyetat(response,this.fnc,"en cours"));       
 

 this.analysefncservice.obs.subscribe((response)=>this.analysefncservice.maptoplanactionbyetat(response,this.analysefncs,"en cours"));
   // the filter 
 this.fnc.forEach((filter) => {
          if (
            !this.analysefncs
              .filter((condition) => condition.n_fnc == filter.n_fnc)
              .some((nc) => nc.etat != "act")
          ) {
            console.log("succes" , filter.n_fnc)

            result.push(filter.n_fnc);
          }
        });
      });

the problem that I get null in result . How to use forkjoin or switchmap to asynch my service call and apply the filter when the calls is end ?

1
  • As far as I can tell, you have an extra set of }); somehow - I'm surprised this compiles for you. Commented Jan 25, 2021 at 21:16

1 Answer 1

1

As you do, you don't wait for the subscribe to complete to apply the filter.

Using forkJoin, as you say, is a good option to wait for the two subscribe to be done.

You can do it like this

const result =[]

forkJoin([
  this.fncservice.obs,
  this.analysefncservice.obs
]).subscribe((result) => {
  this.fncservice.maptofncbyetat(result[0],this.fnc,"en cours")
  this.analysefncservice.maptoplanactionbyetat(result[1],this.analysefncs,"en cours")
  this.fnc.forEach((filter) => {
    if (
      !this.analysefncs
      .filter((condition) => condition.n_fnc == filter.n_fnc)
      .some((nc) => nc.etat != "act")
    ) {
      console.log("succes" , filter.n_fnc)
      result.push(filter.n_fnc);
    }
  });
},(error) => {
  // error message
})

Because of

forkJoin : Accepts an Array of ObservableInput or a dictionary Object of ObservableInput and returns an Observable that emits either an array of values in the exact same order as the passed array, or a dictionary of values in the same shape as the passed dictionary.

I first suppose that this.fncservice.obs and this.analysefncservice.obs are both Observable.

And then, as you can see, in the subscribe of the forkJoin, I send result[0] and result[1] as you did in each subscribe before.

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

3 Comments

Thank u @Alexis for ur reply but I still have the same prblm as before the filter didn"t get applied
@HamzaNouri Then your problem isn't waiting for the service calls. Maybe your filter or one of your other function calls is broken. We can't help you with that at we can't see those.
@HamzaNouri, as @Mrk Sef told, the problem might be in your filter or in your arrays analysefncs or fnc :/

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.