JavaScript

C# async/await vs JavaScript: Same Keywords, Very Different Runtime Guarantees

Both languages give you async and await, and the code often looks identical. A developer who knows one can read the other within minutes. Unfortunately, that familiarity hides a trap.

The keywords are only syntax. What actually matters is the runtime underneath: which thread resumes your code, when it resumes, and what else can run in the meantime. On those questions, C# and JavaScript give nearly opposite answers.

What the Keyword Really Does

In both languages, the compiler splits your method at each await. Everything after the await becomes a continuation, which is a piece of code to run later. The language then needs somebody to run that continuation, and this is where the two worlds diverge.

JavaScript has exactly one answer. C# has a configurable one. Consequently, the same-looking line of code carries very different promises.

JavaScript: One Thread, One Queue

In Node, your code runs on a single thread. When a function hits await, the rest of it is scheduled as a microtask. According to MDN, this happens even when the awaited value is an already-resolved promise or not a promise at all, and execution does not return to the function until the other scheduled microtasks have run.

This design gives you two useful guarantees. First, code between two awaits runs to completion without interruption, so you cannot lose an update to a shared variable inside such a stretch. Second, await always yields, so the order of side effects does not depend on whether the data was ready.

However, the price is steep. Because everything shares one thread, a single long computation freezes timers, I/O callbacks and every other request.

C#: A Thread Pool and a Choice of Scheduler

C# works differently. After an await, the continuation is queued to whatever context was captured. As Stephen Toub explains in the ConfigureAwait FAQ, that means a SynchronizationContext if one exists, otherwise a TaskScheduler, and the default scheduler is the thread pool. A WinForms or WPF app therefore resumes on the UI thread. By contrast, Microsoft’s CA2007 documentation notes that ASP.NET Core has no such context by default, so continuations land on pool threads.

Moreover, C# takes a shortcut that JavaScript refuses to take. If the awaited task has already completed, the method simply keeps running on the same thread, with no yield at all. That is fast, but it changes the order of events.

Seeing the Difference in Ten Lines Each

The two programs below do the same thing: call an async function that awaits something already finished, then print a line from the caller. The C# version needs the .NET 10 SDK, which runs a single file directly. The JavaScript version runs on any recent Node.

cat > Demo.cs <<'EOF'
static async Task Work()
{
  Console.WriteLine("1 before await");
  await Task.CompletedTask;
  Console.WriteLine("2 after await");
}

var t = Work();
Console.WriteLine("3 caller continues");
await t;
EOF
dotnet run Demo.cs
cat > demo.mjs <<'EOF'
async function work() {
  console.log("1 before await");
  await Promise.resolve();
  console.log("2 after await");
}
const p = work();
console.log("3 caller continues");
await p;
EOF
node demo.mjs

In my runs, C# printed the lines in the order 1, 2, 3, because the completed task never suspended the method. Node printed 1, 3, 2, because the await always yielded to the caller first. Same keywords, different order.

Measuring What the Runtime Promises

To go beyond theory, I ran three small experiments on Node 22.23.5 and .NET 10.0.115, using the median of five runs where timing was involved. First, I recorded which threads ran code after awaits across 200 concurrent tasks.

RuntimeDistinct threads after await
Node 221
.NET 10, 1 processor1
.NET 10, 8 processors (set by environment variable)8

Interestingly, .NET on a single processor behaved just like Node, because the pool starts with one worker per processor. Once the pool had eight workers, continuations spread across all of them. In other words, C# gives you parallelism by default, whether or not you asked for it.

Second, I started a 10 ms ticker and then burned 300 ms of CPU inside an async function, measuring the longest gap between ticks.

Own run, median of 5, 300 ms of busy work after an await. The 8-processor case sets DOTNET_PROCESSOR_COUNT=8 on a one-core container, so treat it as an illustration of pool sizing, not a throughput claim.

Node stalled for about 310 ms, which is the whole busy period. With eight pool workers, .NET kept ticking with a worst gap of 16 ms. Notably, with one processor .NET stalled for 173 ms, so the thread pool protects you only when it has spare threads. This is the familiar thread-pool starvation problem in miniature.

Third, I timed an await on an operation that had already completed.

Own run, 20 million awaits per run, median of 5, log scale. These are different runtimes and compilers, so the gap mixes language semantics with implementation. The cause is that JavaScript must queue a microtask every time, while C# can skip suspension.

The C# fast path cost about 1.2 ns per await, against roughly 68 ns for Node. I would not read that as “C# is 50 times faster”. Rather, it shows that the always-yield rule has a real, measurable price, while buying you predictable ordering.

What This Means in Practice

QuestionJavaScript (Node)C# (.NET)
Which thread resumes?Always the single main threadCaptured context, otherwise any pool thread
Await on finished work?Still yields a microtaskContinues synchronously
Shared variables after await?No parallel accessNeeds locks or atomics
Long CPU work?Freezes everythingSpreads over pool threads, until it runs out
Blocking a UI thread?Not applicableCan deadlock on a captured context

The shared-variable row deserves a caveat. Continuations on a thread pool can run in parallel, so unsynchronized writes are unsafe in principle. I tried to reproduce lost updates with a shared counter, but my one-core container produced none in 15 runs, so I have no measured number to show. The risk follows from the threading model, not from my data.

Checklist for crossing between the two

  • In C#, never assume the thread after an await is the one before it.
  • In C# libraries, decide deliberately between ConfigureAwait(false) and the default.
  • In JavaScript, never run heavy synchronous work inside an async function.
  • Do not rely on ordering between a call and its awaited continuation without testing.

What We Have Learned

The keywords match, but the guarantees do not. JavaScript trades parallelism for simplicity: one thread, a microtask on every await, and no data races between awaits, at the cost that any long computation stalls the whole process. C# trades simplicity for flexibility: continuations return to a captured context or run on a thread pool, completed awaits skip suspension, and shared state needs real synchronization. In my tests, Node stalled for about 310 ms during 300 ms of busy work, while a well-sized .NET pool stalled for 16 ms and an undersized one for 173 ms. Therefore, the right habit is to learn the runtime first and the syntax second.

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button