async and await
Language keywords for writing code that waits for slow work, such as a network call, without blocking a thread.
In more detail
An async function returns a task or promise straight away; each await pauses it, frees the thread for other work, and resumes it when the awaited operation completes. Neither keyword creates a thread, so async raises how much work a server keeps in flight rather than making one request faster. Errors from the awaited operation are rethrown at the await, so ordinary try-catch handles them.
Examples
public async Task<Product?> GetProductAsync(int id, CancellationToken ct)C#: the compiler rewrites an async method as a state machine; async methods return Task or Task<T>.In the browser: An async function always returns a promise, and await pauses that function, not the whole page, until a promise settles. The rest of the function resumes as a microtask.export default async function Page() {
const products = await getProducts();
return <List items={products} />;
}React 19 Actions are usually async functions, and startTransition accepts them directly.Related terms
- Task — The .NET type representing an operation that may still be running and will eventually complete with a result, an exception or cancellation.
- thread pool — A fixed or bounded set of reusable worker threads that take tasks from a queue, instead of creating a new thread for each task.
- state machine — A model in which something is always in exactly one named state, such as idle, loading, success or error, and only defined events move it to another.
- CancellationToken — A .NET value passed into async methods that lets the caller signal that the work is no longer wanted and should stop.
- continuation — The rest of a method that is scheduled to run once an awaited task completes.
- Promise — A JavaScript object that stands for a result that is not ready yet and will later either succeed with a value or fail with an error.
- asynchronous — Describes work that starts now and finishes later, letting the program do other things instead of waiting for it.
- microtask — A small job, such as a promise callback, that runs as soon as the current piece of JavaScript finishes, before the event loop handles anything else.
- AsyncLocalStorage — A Node.js feature that keeps a value available to all code running for one request, even across await and callbacks, without passing it as an argument.
- Promise.all — A JavaScript function that starts waiting on several Promises at once and resolves when all of them have finished.
- request waterfall — A chain of requests where each one only starts after the previous one finished, so their waiting times add up.
- try...catch — A statement that runs code in a try block and, if that code throws an exception, runs a catch block instead of crashing.
- top-level await — Using await directly in the body of an ES module, outside any async function, so the module waits for a promise before it finishes loading.
- Action (React) — In React 19, an async function run inside a transition, typically to submit data, with pending state, errors and optimistic updates handled by React.
- Awaited<T> — The utility type that unwraps a promise type to the value it resolves to, repeating through nested promises and thenables.
Taught in
- What is .NET? Runtime vs SDK, and LTS vs STS — Modern .NET Core, C# & Enterprise Microservices
- Async and Await: Managing Concurrency — Modern .NET Core, C# & Enterprise Microservices
- Querying, Change Tracking, and Saving Data — Modern .NET Core, C# & Enterprise Microservices
- The Repository Pattern — Modern .NET Core, C# & Enterprise Microservices
- The Browser Event Loop: Call Stack, Microtasks And Macrotasks — Frontend Architecture & Web Platform
- The Fetch API: Streaming Responses, HTTP Caching And Keepalive — Frontend Architecture & Web Platform
- Database Access & Migrations: ORM & Data Layers — Production Full-Stack Engineering
- API Gateway & BFF Patterns: Backend-for-Frontend — Production Full-Stack Engineering
- Optimistic UI Architecture: Cache & Rollback Protocols — Production Full-Stack Engineering
- Asynchronous Event Processing: Outbox Pattern & Queues — Production Full-Stack Engineering
- Structured Logging & Correlation IDs: Context Stitching — Production Full-Stack Engineering
- End-to-End Testing with Playwright: Multi-Tier Suites — Production Full-Stack Engineering
- App Router Architecture: Server-First Paradigms — Next.js 15 & Modern App Router Architecture
- Server Components: Async Execution & Constraints — Next.js 15 & Modern App Router Architecture
- Dynamic Routes: Slugs & generateStaticParams — Next.js 15 & Modern App Router Architecture
- Granular Suspense: Resolving Waterfall Bottlenecks — Next.js 15 & Modern App Router Architecture
- Not Found Handling: not-found.tsx & Programmatic Exits — Next.js 15 & Modern App Router Architecture
- Async Data Fetching: Parallel Requests & Preloading — Next.js 15 & Modern App Router Architecture
- Stream Mechanics: Readable Streams & Flowing Modes — Node.js Internals & Backend Services
- Stream Pipelines: pipeline, AbortSignal & Teardown — Node.js Internals & Backend Services
- Routing & Middleware: Path Parsing & Async Context — Node.js Internals & Backend Services
- Keys In Depth: Identity Preservation And Identity Resetting — React 19 & Modern Component Architecture
- Diffing Algorithm: Element Type Shifts And Subtree Tear Down — React 19 & Modern Component Architecture
- SyntheticEvent System: Event Delegation in Modern React — React 19 & Modern Component Architecture
- useEffect Lifecycle: Mount, Dependencies, And Cleanup Phases — React 19 & Modern Component Architecture
- Avoiding Effect Antipatterns: Derived State And Events — React 19 & Modern Component Architecture
- useLayoutEffect vs useEffect: Painting And Synchronous Reads — React 19 & Modern Component Architecture
- useRef Mechanics: DOM References And Mutable Persistent State — React 19 & Modern Component Architecture
- useImperativeHandle: Controlled Ref Forwarding Boundaries — React 19 & Modern Component Architecture
- Lifting State Up: Colocation, Ownership, And Data Flow — React 19 & Modern Component Architecture
- useContext in Depth: Scoping, Consumer Subscription Costs — React 19 & Modern Component Architecture
- Union Utilities: Exclude, Extract, NonNullable And Awaited — Production TypeScript
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript