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

78
Views
Early return from a subscribe block in a function

I am new to JavaScript. I am trying to do an early return on error from a subscribe block.

This is what I have

public doSomething() {
   this.updateData(data)
     .subscribe((result) => {
                   // do something
                }, 
                (err) => {
                   console.error("Error");
                   return;
                   // require an early return from the block
                   // so rest of the function isn't executed. 
     });
   // do other work
}

Any pointers will be appreciated.

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

0

The best solution would be:

public doSomething() {
   this.updateData(data)
     .subscribe((result) => {
                   // do something
                   // do other work
                }, 
                (err) => {
                   console.error("Error");
                   return;
                   // require an early return from the block
                   // so rest of the function isn't executed. 
     });
}

But you can also use await with async:

public async doSomething() {
       const result = await this.updateData(data);
       if (!result) return;
       // do other things
         
}
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!