← All writing

JavaScript async/await is syntactic sugar, C# has the real deal

JavaScript async/await is syntactic sugar, C# has the real deal. They look the same in code, but the runtimes couldn’t be more different.

How JS async/await works

  • It makes code look synchronous, but it’s still one thread.
  • I/O-bound tasks (fetch, DB call, file read) → they’re handed off to the event loop which is great, works fine for most.
  • CPU-bound tasks → wrapping in await does nothing, the main thread is still blocked.
  • You can still achieve true concurrency by using Web Workers on the browser or Worker Threads on Node JS.

Bottom line: JS async works for I/O bound tasks, but it is useless for CPU-bound tasks.

Here’s what that means in practice. Marking a function async doesn’t move its work anywhere. The loop below runs on the main thread, and while it runs, every other request to the server just waits. Move the same loop into a worker thread and the event loop stays free:

server.js
app.get('/report', async (req, res) => {
const total = await sumUpTo(1_000_000_000); // every other request waits for this
res.json({ total });
});
async function sumUpTo(n) {
let total = 0;
for (let i = 0; i < n; i++) total += i;
return total;
}
server.js
import { Worker } from 'node:worker_threads';
app.get('/report', async (req, res) => {
const total = await sumUpTo(1_000_000_000); // runs on another thread
res.json({ total });
});
function sumUpTo(n) {
return new Promise((resolve, reject) => {
const worker = new Worker(new URL('./sum-worker.js', import.meta.url), { workerData: n });
worker.once('message', resolve);
worker.once('error', reject);
});
}
sum-worker.js
import { parentPort, workerData } from 'node:worker_threads';
let total = 0;
for (let i = 0; i < workerData; i++) total += i;
parentPort.postMessage(total);

How C# async/await works

  • The compiler rewrites your async method into a state machine.
  • Each await becomes a checkpoint where execution can pause and later resume.
  • I/O-bound work is handed off to the OS, once the work is complete .NET resumes code execution. No thread is held waiting.
  • For CPU-bound work: You explicitly offload with await Task.Run, which uses the ThreadPool. Which means C# async/await is not always single-threaded.
  • For already-completed tasks execution continues synchronously.

While an I/O-bound await is in flight, no thread is sitting there waiting for it. The thread that started the request goes back to the ThreadPool and serves other requests. When the OS reports that the I/O is done, the state machine picks up right after the await, in ASP.NET Core on whichever pool thread is free.

sequenceDiagram
participant M as Your async method
participant P as ThreadPool
participant OS as OS (I/O)
P->>M: run on thread 7 until the first await
M->>OS: start the database query
Note over P: thread 7 goes back to the pool and serves other requests
OS-->>P: I/O completed
P->>M: resume after the await, on any free thread
No thread waits while the query runs
// I/O-bound: no thread is held while the query runs
var orders = await db.Orders.Where(o => o.CustomerId == id).ToListAsync();
// CPU-bound: offload it to the ThreadPool explicitly
var total = await Task.Run(() => SumUpTo(1_000_000_000));

Bottom line: C# async is a scalable concurrency model, not just syntactic sugar.

JavaScript was built for browsers, not scalability. Node.js doesn’t change that, true concurrency lives elsewhere. And for scalable systems, Node.js is not really the right tool.