I like the flatness of the new Async/Await
feature available in Typescript, etc. However, I'm not sure I like the fact that I have to declare the variable I'm await
ing on the outside of a try...catch
block in order to use it later. Like so:
let createdUser
try {
createdUser = await this.User.create(userInfo)
} catch (error) {
console.error(error)
}
console.log(createdUser)
// business
// logic
// goes
// here
Please correct me if I'm wrong, but it seems to be best practice not to place multiple lines of business logic in the try
body, so I'm left only with the alternative of declaring createdUser
outside the block, assigning it in the block, and then using it after.
What is best practice in this instance?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…