Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Can I subscribe to method which is running forkJoin inside

I have pretty basic question. I do have a method which wraps forkJoin call

ngOnInit(): void {
    getCustomData();
    forkJoin([
        this.serviceTwo.test(),
        this.serviceThree.test()
    ])
    .pipe(doSomeStuff())
    .subscribe([test1, test2=> {
        //someActions
    })
}

getCustomData(): void {
    forkJoin([
        this.service.doOne(),
        this.service.doTwo()
    ])
    .pipe(doSomeStuff())
    .subscribe([one, two] => {
        //someActions
    })
}

And what I would like to try to do is subscribe to getCustomData() and do calls to serviceTwo, serviceThree after getCustomData is finished.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If I understood correctly what you're trying to do the solution could be like this:

ngOnInit(): void {
    getCustomData()
    .pipe(switchMap([one, two] => {
        //someActions
        return forkJoin([
            this.serviceTwo.test(),
            this.serviceThree.test()
           ])
        }),
        doSomeStuff()
    ).subscribe(([test1, test2])=> {
        //someActions
    })
}

getCustomData(): Observable<any>{
    return forkJoin([
        this.service.doOne(),
        this.service.doTwo()
    ])
    .pipe(doSomeStuff())
}

So now you're waiting that everything is finished in getCustomData, you call switchMap in the pipeline and before returning the new Observable (the forkJoin with test methods) you do what you usually do in the getCustomData subscribe. Lastely you have to just subscribe to the whole thing, as you already do.

Check switchMap for more info.

about 4 years ago · Juan Pablo Isaza Report

0

If I understand the problem right, you need to do perform this.service.doOne() and this.service.doTwo() in parallel, wait for the completion of both and THEN perform serviceTwo and serviceThree.

If this is the case, then I would proceed like this

// getCustomData returns an Observable that represents the execution of
// doOne() and doTwo() in parallel followed by doSomeStuff()
// By the way, you have to make sure that doSomeStuff() returns a pipeable operator
// which accepts [resultOfDoOne, resultOfDoTwo] as input parameters
// It is important NOT TO SUBSCRIBE here, you just return the Observable
getCustomData() {
    return forkJoin([
        this.service.doOne(),
        this.service.doTwo()
    ])
    .pipe(doSomeStuff())
}

ngOnInit(): void {
    // call getCustomData() to get the Observable
    getCustomData()
    // now you can pipe something into that Observable
    .pipe(
      // since you want to execute first getCustomData() and then serviceTwo
      // and serviceThree, you have to concatenate the 2 Observable
      concatMap(() => forkJoin([
        this.serviceTwo.test(),
        this.serviceThree.test()
       ])),
       // now you do some stuff, again make sure that doSomeStuff() returns a 
       // pipeable operator that accepts resultOfServiceTwo and resultOfServiceThree
       doSomeStuff()
    )
    // eventually you subscribe
    .subscribe([test1, test2=> {
        //someActions
    })
}

In the above implementation be careful about what doSomeStuff should do.

If doSomeStuff just implements a side effect, i.e. it just does something with the values notified by the upstream, then you probably should look at the tap operator, but this is difficult to guess from the code you have shown.

about 4 years ago · Juan Pablo Isaza Report

0

    ngOnInit(): void {
        this.getCustomData();
    }
    
    getCustomData(): void {
        forkJoin([
            this.service.doOne(),
            this.service.doTwo()
        ])
        .pipe(doSomeStuff())
        .subscribe([one, two] => {
            this.getServiceTwoThreeData();
        })
    }

    getServiceTwoThreeData() {
       forkJoin([
            this.serviceTwo.test(),
            this.serviceThree.test()
        ])
        .pipe(doSomeStuff())
        .subscribe([test1, test2=> {
            //someActions
        })
}

You can simply add your service two, service three forkJoin to a method and call it from subscribe.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!