Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials TypeScript Promises & Async/Await
TypeScript Intermediate FREE

Promises & Async/Await

Lesson 1 of 17 Intermediate Interactive

TypeScript works with Promises and async/await for asynchronous operations.

Syntax

TYPESCRIPT
async function fetchData(): Promise<Data> {
    const response = await fetch("/api/data");
    return response.json();
}
Promises and Async/Await
TYPESCRIPT
function fetchData(): Promise<string> {
    return new Promise((resolve) => {
        setTimeout(() => resolve("Data loaded"), 1000);
    });
}

async function getData(): Promise<void> {
    console.log("Loading...");
    let result = await fetchData();
    console.log(result);
    console.log("Done!");
}

getData();