可以省略这个捕获块吗?对于这个Promise安排?

huangapple go评论72阅读模式
英文:

Can I omit this catch block for this promise arrangement?

问题

I'm trying to use best practice. And per my understanding you only need a catch clause at the end of a promise chain.

However this is not a straightforward chain. createUser() is in a then method. However, should not any errors in createUser() be caught in getOrCreateUser()?

Can I omit the catch clause in createUser()?

function getOrCreateUser (accessToken, refreshToken, profile, done) {
  const user_props = obtainUserPropsFromGoogle(profile);
  DBM.getUser(user_props.id_google).then( (res) => {
    return res[0] ? done(null, user_props) : createUser(done, user_props);
  }).catch( error => {
    return done(error, null);
  });
}

function createUser (done, user_props) {
  DBM.createUser(user_props).then(() => {
    return done(null, user_props);
  }).catch( error => {
    return done(error, null);
  });
}
英文:

I'm trying to use best practice. And per my understanding you only need a catch clause at the end of a promise chain.

However this is not a straight forward chain. createUser() is in a then method. However should not any errors in createUser() be caught in getOrCreateUser()?

Can I omit the catch clause in createUser()?

function getOrCreateUser (accessToken, refreshToken, profile, done) {
  const user_props = obtainUserPropsFromGoogle(profile);
  DBM.getUser(user_props.id_google).then( (res) => {
    return res[0] ? done(null, user_props) : createUser(done, user_props);
  }).catch( error => {
    return done(error, null);
  });
}

function createUser (done, user_props) {
  DBM.createUser(user_props).then(() => {
    return done(null, user_props);
  }).catch( error => {
    return done(error, null);
  });
}

答案1

得分: 1

不能,按照代码的写法,不能省略createUser()中的catch子句。

为了移除内部 promise 中的catch外部 promise 需要处理内部 promise 的拒绝。

为了实现这一点,你需要从createUser函数中返回内部 promise。然后,你需要从外部 promise 上的then处理程序中返回该 promise。

你实际上有如下代码:

Promise.resolve().then(() => {
  Promise.reject()
}).catch(/* 不能正确处理上面的 Promise.reject */)

你所需要的是这个:

Promise.resolve().then(() => {
  return Promise.reject();
}).catch(/* 正确处理上面的 Promise.reject */)

在实际情况下,你的代码应该如下所示:

function getOrCreateUser(accessToken, refreshToken, profile, done) {
  const user_props = obtainUserPropsFromGoogle(profile);
  DBM.getUser(user_props.id_google).then((res) => {
    // 现在`createUser`返回其 promise,这个`return`实际上是有用的
    return res[0] ? done(null, user_props) : createUser(done, user_props);
  }).catch(error => {
    done(error, null);
  });
}

function createUser(done, user_props) {
  // 必须将这个 promise 从`createUser`中返回
  return DBM.createUser(user_props).then(() => {
    done(null, user_props);
  })
}
英文:

> Can I omit the catch clause in createUser()?

No, not the way the code is written.

In order to remove the catch from the inner promise, the outer promise needs to handle the rejection of the inner promise.

For that to happen, you need to return the inner promise from createUser. Then, you need to return that promise from the then handler on the outer promise.

You effectively have this:

Promise.resolve.then(() => {
  Promise.reject()
}).catch(/* cannot handle Promise.reject above */)

What you need is this:

Promise.resolve.then(() => {
  return Promise.reject();
}).catch(/* correctly handles above Promise.reject */)

In practical terms, your code should look like this:

function getOrCreateUser (accessToken, refreshToken, profile, done) {
  const user_props = obtainUserPropsFromGoogle(profile);
  DBM.getUser(user_props.id_google).then( (res) => {
    // Now that `createUser` returns its promise, this `return` is actually useful
    return res[0] ? done(null, user_props) : createUser(done, user_props);
  }).catch( error => {
    done(error, null);
  });
}

function createUser (done, user_props) {
  // This promise must be returned out of `createUser`
  return DBM.createUser(user_props).then(() => {
    done(null, user_props);
  })
}

huangapple
  • 本文由 发表于 2020年1月6日 23:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定