Async, short for asynchronous, means a program can start a slow task, such as a download or a file read, and keep doing other work while it waits for the result. When the task finishes, the program picks up the result and continues from where it paused.
The opposite is synchronous (sync) code, where each line must finish before the next one begins. This article explains the idea without tying it to one language, then shows the async and await keywords in JavaScript, Python and C#.
What does async mean, in one example?
Think of two ways to do a task that has a long wait in the middle, such as downloading a file and then showing its size.
- Synchronous: request the file, stand still until every byte arrives, then show the size. Nothing else happens during the wait.
- Asynchronous: request the file, go on doing other things (respond to clicks, update the screen, start other requests), and come back to show the size when the download is done.
MDN defines it the same way: asynchronous programming "enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result."
What does sync mean, and why does blocking matter?
MDN describes synchronous code as a program that steps through the code one line at a time, in the order written, and waits for each line to finish before going on. A function is synchronous when the caller has to wait for it to finish and return a value before the caller can continue.
That is fine for fast operations. It becomes a problem when one line is slow. MDN's example is a page with a button that runs a long calculation: while the calculation runs, the program is "completely unresponsive: you can't type anything, click anything, or do anything else." MDN explains the cause: the JavaScript program is single-threaded, so it can only do one thing at a time. The same thing happens in desktop apps. Microsoft's C# documentation notes that if a process on the UI thread is blocked, all are blocked, and the application stops responding, so you might conclude that it failed when instead it's just waiting.
The word for this is blocking. A blocking call holds the thread until it returns. A non-blocking, asynchronous call returns control right away and delivers the result later.
What do the async and await keywords do?
Most modern languages give you two keywords for this, and they work the same way in broad terms:
asyncmarks a function as asynchronous. Calling it starts the work and returns a placeholder for the eventual result, rather than the result itself.awaitpauses that function at one point until the placeholder has a result. While it is paused, other code can run. When the result is ready, the function resumes on the next line.
The placeholder has a different name in each language. It is a Promise in JavaScript, a coroutine object in Python (with Task and Future objects as other awaitable types), and a Task in C#.
Async in JavaScript
async function fileSize(url) {
const response = await fetch(url); // pauses here, not the whole program
const text = await response.text();
return text.length;
}
fileSize("/data.txt").then(size => console.log(size));
console.log("this line runs first");
The last line prints before the size does, because fileSize pauses at its first await and hands control back to the caller. An async function always returns a promise, so the caller uses .then() or its own await to get the number. The JavaScript async/await guide covers errors, parallel requests and the common forEach mistake in detail.
Async in Python
Python's asyncio library uses async def to define a coroutine function. This example comes from the Python documentation, with its output:
import asyncio
async def main():
print('hello')
await asyncio.sleep(1)
print('world')
asyncio.run(main())
Example output:
hello
world
Two details trip people up. First, the docs state that simply calling a coroutine will not schedule it to be executed: main() on its own gives you <coroutine object main at 0x...> and nothing runs. You need await inside another coroutine, or asyncio.run() at the top level. In the documentation's own words, a coroutine that is created but never awaited "won't run at all", and Python raises a RuntimeWarning.
Second, awaiting one call after another is still sequential. To run several awaitables at the same time, use asyncio.gather(), which the docs describe as running awaitables "concurrently":
results = await asyncio.gather(fetch_a(), fetch_b(), fetch_c())
If each of those three calls waits one second, awaiting them one by one takes about three seconds in total, while gather takes about one, because the waits overlap.
Async in C#
C# uses the same two keywords. By convention an async method returns Task or Task<T> and its name ends in Async:
public async Task<int> GetUrlContentLengthAsync()
{
using var client = new HttpClient();
string contents = await client.GetStringAsync("https://learn.microsoft.com/dotnet");
return contents.Length;
}
Microsoft's documentation describes what await does here: it suspends the method, control returns to the caller, and control resumes when the awaited task is complete. It also gives a useful caution: an async method that contains no await runs as a synchronous method, despite the async modifier, and the compiler warns about it.
Is async the same as multithreading?
No, and this is the most common confusion. Async is about not waiting idly. Multithreading is about running code on several threads at once.
Microsoft's documentation is explicit: the async and await keywords don't cause extra threads to be created, and an async method doesn't run on its own thread. It runs on the current context and uses time on the thread only when the method is active. JavaScript makes the point from the other direction: a single thread, yet asynchronous code throughout.
The practical consequence is that async helps with waiting (network, disk, timers) and does nothing for computing. MDN notes that wrapping a heavy calculation in a promise does not move it to another thread, so it still blocks the main thread while it runs. For CPU-heavy work you need a separate thread or process (Web Workers in browsers, Task.Run in C#, a process pool in Python).
When should you use async?
Use it when the program spends most of its time waiting for something outside the processor:
- HTTP requests and API calls.
- Reading and writing files or databases.
- Timers and delays.
- Anything on a UI thread, where a blocked thread freezes the window.
Skip it for short, quick operations and for pure calculation. Async code adds a second way to fail: forgetting an await leaves you holding a placeholder instead of a value, and errors surface at the await, not at the call.
If async code doesn't behave
- You print a Promise, coroutine object or Task instead of a value. You called an async function without awaiting it. Add
await(or.then()in JavaScript). - Python says
RuntimeWarning: coroutine ... was never awaited. A coroutine was created and dropped. Await it, or pass it toasyncio.run()orasyncio.gather(). awaitis rejected as a syntax error. The keyword only works inside an async function (or at the top level of a JavaScript module). Mark the enclosing functionasync.- The page still freezes. The slow part is computation, not waiting. Async will not fix that; move the work to a worker thread or process.
Frequently asked questions
- Is async the same as multithreading?
- No. Async means a task can pause and let other work run while it waits. Multithreading means several threads run code at the same time. Microsoft's C# documentation states that the async and await keywords do not create extra threads, and that an async method does not run on its own thread. JavaScript programs are single-threaded and still use async code heavily.
- Is async the same as asynchronous?
- Yes. Async is the short form of asynchronous. In many languages it is also a keyword (async in JavaScript, Python, C# and Rust) that marks a function as one that can pause at await points.
- Does async make my code faster?
- Not by itself. Async helps when a program spends time waiting for something outside the CPU, such as a network response or a disk read, because other work can happen during the wait. It does not speed up heavy calculation, which keeps the processor busy the whole time.
- What is the opposite of async?
- Synchronous, often shortened to sync. In synchronous code each line finishes before the next one starts, so a slow line holds up everything after it.
- Why does my async function return a promise or coroutine instead of a value?
- Calling an async function starts the work and hands back a placeholder for the result: a Promise in JavaScript, a coroutine object in Python, a Task in C#. You get the real value by awaiting that placeholder.