# Typing Promises & Async

> Wrapping return values in the Promise<T> generic.

Subject: TypeScript | Lesson 20 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/typing-promises-async
Previous: [Function Overloads](https://codewithanimation.in/typescript/function-overloads.md) | Next: [Function Variance](https://codewithanimation.in/typescript/function-variance.md)

## The async guarantee

In JavaScript, every function marked with the `async` keyword implicitly wraps its return value in a Promise.

Therefore, in TypeScript, the return type of any `async` function must always be `Promise<T>`, where `T` is the type of the value that will eventually be returned.

## Preventing synchronous assumptions

If you forget to `await` an async function, you don't get the user data—you get a pending Promise object. TypeScript forces you to type it as `Promise<User>`, so if you try to access `user.name` without awaiting, the compiler will stop you.

## Syntax

```typescript
type User = { id: number; name: string };

// 1. Explicit return type: Promise<User>
async function fetchUser(): Promise<User> {
  const response = await fetch("/api/user");
  return response.json();
}

async function main() {
  const userPromise = fetchUser();
  // Error: Property 'name' does not exist on type 'Promise<User>'
  // console.log(userPromise.name); 

  const user = await fetchUser();
  // OK: user is now successfully unwrapped to 'User'
  console.log(user.name);
}
```

## Common mistake: The 'Awaited' utility

Sometimes you are given a Promise type (e.g. `type P = Promise<string>`) but you need to know what it resolves to.

You can use the built-in `Awaited<T>` utility type to "unwrap" the promise at the type level:

`type Resolved = Awaited<Promise<string>>; // Result: string`

## Tip: Void Promises

If an async function doesn't return any data (like a simple delay function), its return type should be `Promise<void>`.

## Try it: Try it

Watch how a value is wrapped inside a `Promise<T>` box, and how the `await` keyword unpacks the box to retrieve the value `T`.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/typing-promises-async

## Check yourself

### Question 1

What happens if you type an async function with 'async function getData(): string'?

- A. It compiles correctly and returns a string.
- B. TypeScript throws an error because all async functions must return a Promise type.
- C. It converts the string into a synchronous return value.

Correct answer: B

TypeScript models the JavaScript runtime. Since JS async functions always return Promises, TS requires you to type it as Promise<string>.

- Why not A: Async functions always return Promises in JS.
- Why not C: You cannot change runtime behavior with TS types.

## Remember this

- Every `async` function must return a `Promise<T>`.
- Awaiting a `Promise<T>` unwraps it and gives you `T`.
- If an async function returns nothing, use `Promise<void>`.
- Use the `Awaited<T>` utility to extract the resolved type from a Promise.
