英文:
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);
})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论