Home > Web Front-end > JS Tutorial > How to Correctly Use Try...Catch with Async/Await in TypeScript?

How to Correctly Use Try...Catch with Async/Await in TypeScript?

DDD
Release: 2024-12-09 20:10:16
Original
493 people have browsed it

How to Correctly Use Try...Catch with Async/Await in TypeScript?

Correct Try...Catch Syntax Using Async/Await

When leveraging the asynchronous nature of Async/Await in languages like TypeScript, it's essential to use the correct syntax for error handling. One common question arises regarding the placement of variables to be awaited within try...catch blocks.

Best Practice

It's generally considered best practice to place the variable declaration inside the try block and assign its value there. This allows for error handling within the scope of the variable's creation and ensures that it always contains a valid 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
}
Copy after login

Alternatives to External Declaration

If you desire to handle errors only from the within the promise assignment, you have three alternatives:

  1. Test for Variable Assignment: Declare the variable outside the block and check for its assignment:
let createdUser;
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
}
Copy after login
  1. Error Type Testing: Test the caught exception for its type and handle it accordingly:
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;
    }
}
Copy after login
  1. Then Callbacks: Use the then method with two callbacks instead of a try/catch block:
await this.User.create(userInfo).then(createdUser => {
    // user was successfully created
    console.log(createdUser);
    // business logic goes here
}, error => {
    console.error(error); // from creation
});
Copy after login

Each alternative has its pros and cons, so consider the specific requirements of your application when choosing the most appropriate approach.

The above is the detailed content of How to Correctly Use Try...Catch with Async/Await in TypeScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template