TypeScript with Async and Promises
Use TypeScript to strongly type async functions, Promises, and API calls for safer async code.
Async code is everywhere in real apps. ## Typed Promise ```ts function fetchCount(): Promise<number> { return Promise.resolve(10); } ``` ## Async function ```ts async function getUser(): Promise<{ id: number; name: string }> { return { id: 1, name: "Tom" }; } ``` ## Using await ```ts async function main() { const user = await getUser(); console.log(user.name); } ``` ## Error handling ```ts async function load() { try { const n = await fetchCount(); console.log(n); } catch (e) { console.error(e); } } ``` ## Graph ```mermaid flowchart TD A[Async fn] --> B[Promise] B --> C[await] C --> D[Value] ``` ## Remember - Always type Promise<T> - Async returns Promise automatically