Try It Right ... Catch Syntax Using Async / Await

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) // business // logic // goes // here 

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?

+26
javascript promise try-catch async-await ecmascript-2017
source share
2 answers

It seems best to not put a few lines of business logic in the try body.

In fact, I would say that it is so. Usually you want to catch exclude all exceptions from working with a value:

 try { const createdUser = await this.User.create(userInfo); console.log(createdUser) // business logic goes here } catch (error) { console.error(error) // from creation or business logic } 

If you want to catch and handle errors only from a promise, you have three options:

  • Declare a variable outside and a branch depending on whether there was an exception or not. This can take many forms, for example

    • assign a default value to a variable in a catch
    • return early or repeated throw exception from catch
    • set the flag whether the catch block caught an exception and checked it if
    • check the value of the variable that was assigned

     let createdUser; // or use `var` inside the block try { createdUser = await this.User.create(userInfo); } catch (error) { console.error(error) // from creation } if (createdUser) { // user was successfully created console.log(createdUser) // business logic goes here } 
  • Check the caught exception for its type and process it or repair it based on this.

     try { const createdUser = await this.User.create(userInfo); // user was successfully created console.log(createdUser) // business logic goes here } catch (error) { if (error instanceof CreationError) { console.error(error) // from creation } else { throw error; } } 

    Unfortunately, standard JavaScript (as before) does not support syntax for conditional exceptions .

  • Use then with two callbacks instead of try / catch . This is really the least ugly way and my personal recommendation is also for its simplicity and correctness, without relying on tagged errors or views on the result, to distinguish between fulfillment and rejection of a promise:

     await this.User.create(userInfo).then(createdUser => { // user was successfully created console.log(createdUser) // business logic goes here }, error => { console.error(error) // from creation }); 

    Of course, this is due to the lack of introducing callback functions, which means that you cannot break / continue doing loops or doing early return from an external function so easily.

+20
source share

Another simpler approach is to add .catch to the promise function. eg:

 const createdUser = await this.User.create(userInfo).catch( error => { // handle error }) 
0
source share

All Articles