By freedomflyer

2017-06-20 22:15:16 8 Comments

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 awaiting 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) {

// 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?

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] How and when to use ‘async’ and ‘await’

20 Answered Questions

[SOLVED] Using async/await with a forEach loop

46 Answered Questions

[SOLVED] What is the preferred syntax for defining enums in JavaScript?

3 Answered Questions

10 Answered Questions

[SOLVED] Combination of async function + await + setTimeout

6 Answered Questions

[SOLVED] Syntax for async arrow function

7 Answered Questions

[SOLVED] try/catch blocks with async/await

9 Answered Questions

[SOLVED] Await in catch block

5 Answered Questions

[SOLVED] Try-catch speeding up my code?

1 Answered Questions

Sponsored Content