#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
3 min read
Question 40 of 48easy

What is the Awaited utility type?

Unwrapping Promise types.

What You'll Learn

  • Awaited<T> usage
  • Nested Promise handling
  • Practical examples

Awaited<T>

Unwraps Promise types to get the resolved value type.

code.tsTypeScript
type P = Promise<string>;
type S = Awaited<P>;
// string

type Nested = Promise<Promise<number>>;
type N = Awaited<Nested>;
// number (fully unwrapped)

With async Functions

code.tsTypeScript
async function fetchUser(): Promise<{ name: string }> {
  return { name: "John" };
}

type User = Awaited<ReturnType<typeof fetchUser>>;
// { name: string }

Practical Examples

code.tsTypeScript
// Multiple async functions
async function getUsers() {
  return [{ id: 1 }, { id: 2 }];
}

async function getPosts() {
  return [{ title: "Hello" }];
}

type Results = Awaited<ReturnType<typeof getUsers>>;
// { id: number }[]

// Promise.all inference
async function fetchAll() {
  const [users, posts] = await Promise.all([
    getUsers(),
    getPosts()
  ]);
  // users: { id: number }[]
  // posts: { title: string }[]
}

Non-Promise Types

code.tsTypeScript
type A = Awaited<string>;
// string (unchanged)

type B = Awaited<string | Promise<number>>;
// string | number