Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Async/Await with Types | TypeScript Mastery lesson | BraveProgrammer
  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 24 of 56

·Typescript

Async/Await with Types

20 minutes

Course progress43%

Async Functions

async function delay(ms: number): Promise<void> {
  return new Promise( (resolve, ms));
}

  (): <> {
   ();
   ;
}
resolve =>
setTimeout
async
function
fetchData
Promise
string
await
delay
500
return
"done"

Return Types

function getNumber(): Promise<number> { /* … */ }

If you forget to await, the function returns a Promise<...> which may cause type mismatches.

Error Handling

async function safeFetch(url: string): Promise<string | null> {
  try {
    const r = await fetch(url);
    if (!r.ok) throw new Error(r.statusText);
    return await r.text();
  } catch (e) {
    console.error(e);
    return null;
  }
}

Parallel Await with Promise.all

async function loadAll(urls: string[]): Promise<string[]> {
  const promises = urls.map(u => fetch(u).then(r => r.text());
  return Promise.all(promises);
}

Info

When using Promise.all, the resulting type is inferred as an array of the individual promise results.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from TypeScript MasteryBrowse activities
Previous lessonWorking with JSON & External APIsContinue to lesson 25Promises & GenericsBack to TypeScript Mastery curriculum