简体   繁体   English

承诺链中的异常处理

[英]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 => {})

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 :请注意,这意味着后续履行处理app中的app将是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 => {})

Answer posted before, is correct.之前过的答案,是正确的。

I just want to insert my 2 cents.我只想插入我的 2 美分。

You can have some fun with one helper function (or use something more fancy like whole Either monad thingy from fp-ts package)你可以用一个辅助函数来获得一些乐趣(或者使用更花哨的东西,比如fp-ts包中的整个Either monad东西)

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并在没有try\\catch.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.或者更无用的方法,您可以抛出自定义错误,因此您的第一个.catch块将能够执行某些操作。

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 */})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM