I like the plane of the new Async/Await available in Typescript, etc. However, I'm not sure that I like the fact that I have to declare the variable I'm await ing outside the try...catch in order to use it later. For example:
let createdUser try { createdUser = await this.User.create(userInfo) } catch (error) { console.error(error) } console.log(createdUser)
Please correct me if I am wrong, but itโs best not to place a few lines of business logic in the try body, so I will only stay with the createdUser declaration alternative outside the block, assigning it in the block, and then using it after.
What is the best practice in this case?
javascript promise try-catch async-await ecmascript-2017
freedomflyer
source share