Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

angular - How Subscribe to multiple async http calls and apply filter

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 ?

question from:https://stackoverflow.com/questions/65883204/how-subscribe-to-multiple-async-http-calls-and-apply-filter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...