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

266
Views
How do I run code after returned function subscription?

I have the following code that looks like this:

foo(){
   return this.service.asyncFunction();
}

//in another function:

this.foo().subscribe(() => { some_code });

Is it possible to refactor the foo function too look like this:

foo(){
   return this.service.asyncFunction().subscribe();
}

But still be able to run some_code after the subscription?

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

0

If you want to subscribe inside foo() and still be able to send the observable you can do this

foo(){
   const myObs = this.service.asyncFunction();
   myObs.subscribe();
   return myObs;
}

Somewhere else in the code, you can do this

this.foo().subscribe(() => { some_code });

But if you want to use foo() and give it some code then you can do this

foo(callback){
   this.service.asyncFunction().subscribe(callback);
}

And somewhere else in the code, do this

for(() => { some code });
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!