Async code uses try and catch around awaited work that might fail.

Async Error Handling

errors.ts
async function loadUser(allowUser: boolean): Promise<string> {
    if (!allowUser) {
        throw new Error("missing user");
    }
    return Promise.resolve("Ada");
}

const allowUser: boolean = ;

async function main(): Promise<void> {
    try {
        const user: string = await loadUser(allowUser);
        console.log(`user=${user}`);
    } catch (error) {
        console.log("user=guest");
    }
}

main();
async function loadUser(allowUser: boolean): Promise<string> {
    if (!allowUser) {
        throw new Error("missing user");
    }
    return Promise.resolve("Ada");
}

const allowUser: boolean = ;

async function main(): Promise<void> {
    try {
        const user: string = await loadUser(allowUser);
        console.log(`user=${user}`);
    } catch (error) {
        console.log("user=guest");
    }
}

main();
async error A rejected promise becomes an exception at the `await` line and can be handled with `catch`.