Simple subscribe within the resolver in Angular 11. How to wait until subscribe is complete so I can do a comparison of data

JSkyS

This is different from other questions because I am trying to have the subscribe within the map() method.

I have started working with a resolver recently and still a little iffy in regards to how the async functionality works between the pipe and the subscribe.

I have a method getAssociatedPartners which will return an array of objects. [{partnerId:1},{partnerId:10}]. Within this method is a subscribe which will call the backend to get this data.

I also have a resolve method which makes another call to another db using getPartnerById, and gets one object {id:1, partnerId:10}.

Once the subscribe from getAssociatedPartners is complete, and data from getPartnerById is complete, I would like to see if partnerId in the array from getAssociatedPartners and this.partner.id has a match.
If so then I would like to continue and return the result as it is on the line --> return partner as Partner. If not then I will redirect.

How can I make the method this.getAssociatedPartners wait until the data is returned so that I can use var partnerIds to do comparisons with this.partner?

Currently it does not wait, and passes up the method and finishes the resolver before the data is returned. "here2" gets console.logged before the this.getAssociatedPartners has data returned.

      resolve(route: ActivatedRouteSnapshot): Observable<Partner> {
    return this.aService.getPartnerById(route.params['id']).pipe(
      map((partner) => {
        var partnerIds = this.getAssociatedPartners(route.params['seoUrl']);
        console.log("here2")
            this.partner = partner;
            if (this.partner === undefined) {
                this.router.navigate(['**']);
              } else {
                if (!this.partner.website.includes('http')) {
                  this.partner.website = 'https://' + this.partner.website;
                }
              }
          return partner as Partner;
      }),
      catchError(() => {
        this.router.navigate(['/error']);
        return EMPTY;
      })
    );
  }

  getAssociatedPartners(seoUrl:string) : object[]{
      console.log(seoUrl)
    this.VHService.getPartnerIds(seoUrl).subscribe((result) =>{
        this.partnerIds = result.data;
        console.log("here1")
    })
    return this.partnerIds;
  }
will-cpu

I think you can use forkJoin to combine the 2 observables and return the one you want. I dont have the same setup as you so you might need to modify this code.

This should allow you to run both observables and compare their results.

Let me know if this helps.


resolve(route: ActivatedRouteSnapshot): Observable<Partner> {
return forkJoin(
    // as of RxJS 6.5+ we can use a dictionary of sources
    {
      p1: this.aService.getPartnerById(route.params['id']),
      p2:  this.VHService.getPartnerIds(route.params['seoUrl'])
    }
  ).pipe(
    map((partners) => {
      /*  logic 
          Access both observables as like an map
          partners.p1
          partners.p2
      */
      return partners.p1 as Partners;
    }),
    catchError(() => {
      this.router.navigate(['/error']);
      return EMPTY;
    })
  );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to wait for subscribe to finish?

How can i subscribe to multiple observables in angular2 at once and wait, if there is new data on each of them?

How can I simply return boolean with angular 6 .subscribe()

How to wait subscribe until all finished?

How do I unit test with a subscribe in Angular5?

How can I subscribe to iframe location changes in an angular app?

Angular Subscribe within Subscribe

How to conditionally subscribe inside an observable, wait for response and return the complete result?

how can I implement subscribe in html template in angular7

How can i subscribe data with forkJoin in angular 10?

Angular: How can I alter a return inside a subscribe?

In tcsh, how can I fork multiple shell commands, wait until they all complete, and then do another command?

How can I wait until my AJAX request is complete?

How do I subscribe to a bug?

How do I make an activity wait until Facebook login is complete?

How Can I Subscribe to FormControl Value Changes In an Angular ControlValueAccessor Directive

How do we subscribe to the currently shown data in Angular Material Tables

Angular wait until subscribe is done and give values to other function

How i can wait until the subscribe finish before return a value of another function

Wait until a subscribe is called in angular

Angular 11 wait until subscribe finishes getting data

How to wait until all data is received to complete function in typescript/angular?

Angular 11 - subscribe is deprecated: Use an observer instead of a complete callback

How can I map through objects in angular with subscribe method?

"If" condition within Subscribe - Angular

How can I subscribe or merge new Observable in RXJS and Angular

How to use multiple callback / subscribe / wait until a method finish in angular 13?

How can I get the value of an API result in Angular subscribe()?

How can I do: subscribe to the data listened from a publisher, make changes and publish it again? [ ROS - Python]