Catching Asynchronous Exceptions

This lesson describes how to handle asynchronous code exceptions.

Asynchronous code

An asynchronous exception occurs when an asynchronous code throws an exception. Asynchronous code is often associated with the concept of Promise that is defined in the next section. In TypeScript (as well as JavaScript), asynchronous code is a piece of code that executes aside from the main thread of execution. The goal is to have a task running while another task is executed. For example, you can click buttons and write inside inputs while a response to a server is fetching information.

Promises

A Promise is the creation of asynchronous code. The mechanism allows calling the following function when a problem occurs or when there is a successful promise completed. In the following code, line 3 throws an error. The problem is that no code handles the error, hence it will get an unhandled exception.

The following code executes unsuccessfully:

Get hands-on with 1400+ tech skills courses.