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

178
Views
Exception handling in promise chain

I have a code with multiple promise chain as shown below

.then(function(response) {
    //my code   
})
.then(function(app) {
    //my code
})
.then(function() {
    //my code   
})

Have added exception handling to each of them as shown below so that if one breaks the rest chain continues.

Is this the correct way of handling exception for multiple chain blocks, or any best practice can be followed to handle the exceptions so that the code execution doesn't break if one fails.

.then(function(response) {
    //my code   
})
.catch(e => {})

.then(function(app) {
    //my code
})
.catch(e => {})

.then(function() {
    //my code   
})
.catch(e => {})
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If your code can accommodate the error (whatever it is) that's occurring early on, this can be a reasonable way of doing it, but I'd always have to look twice at it in a code review because it's fairly unusual for the code to be able to just ignore errors like that. The code is roughly equivalent to:

try {
    //my code   
} catch (e) {
}

try {
    //my code
} catch(e) {
}

try {
    //my code   
} catch(e) {
}

...but using promises instead. So it's a bit suspect, but can be correct, for the same reasons the above is a bit suspect but can be correct if you need to do a series of things, one at a time, and have each of them done even if the previous one fails.

Beware that it means app in the subsequent fulfillment handler will be undefined:

.then(function(response) {
    //my code   
})
.catch(e => {})

.then(function(app) { // <=== `app` is `undefined` here
    //my code
})
.catch(e => {})

.then(function() {
    //my code   
})
.catch(e => {})
about 4 years ago · Juan Pablo Isaza Report

0

Answer posted before, is correct.

I just want to insert my 2 cents.

You can have some fun with one helper function (or use something more fancy like whole Either monad thingy from fp-ts package)

const run = async (fn) => {
    try {
       const result = await fn()
       return [result, null]
    } catch (err) {
        return [null, err]
    }
}

and write code without try\catch or .then\.catch

const [response, error] = await run(() => fetch('asdfasdf'))
const app = buildApp(response.ok ? await response.json() : { fallback: 'data' })

const [appResponse, appError] = await run(async () => {
  await app.compileTemplate()
  return app.buildResponse()
})

if (appResponse) {
 // ...
}

Or more useless approach, you can throw a custom error, so your first .catch block will be able to do something.

class AppFromResponseError extends Error {
  constructor(message, fallbackApp) {
    super(message)
    this.name = "ResponseError"
    this.app = "fallbackApp"
  }
}

builder
  .then(function(response) {
    if (response.ok !== true) 
      throw new AppFromResponseError('not ok', minimalApp)
  })
  .catch(e => {
    if (e.app) return e.app
  })
  .then(app => { /* some kind of app will be here */})
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!