Async Methods Confusion
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Async/await syntax in C# and JavaScript simplifies asynchronous programming but introduces common confusions: blocking on async code (.Result/.Wait() causing deadlocks), forgetting to await an async call (fire-and-forget), misunderstanding what async actually does, and confusion about when code runs on which thread. The key insight is that async does not make code run on a background thread — it allows the current thread to be released during await and resumed later, enabling non-blocking I/O operations.
What async/await Actually Does
The compiler transforms async methods into state machines. When await is reached, the method returns a Task and the calling code continues. When the awaited operation completes, the state machine resumes from where it left off.
Confusion 1: Forgetting to Await
Confusion 2: Blocking on Async Code
The deadlock occurs in environments with a SynchronizationContext (ASP.NET, WPF, WinForms). The .Result call blocks the context thread, and the await continuation needs that same thread to resume, creating a circular dependency.
Confusion 3: async void vs async Task
async void should only be used for event handlers (e.g., button_Click). For all other cases, return Task or Task<T> so callers can await, catch exceptions, and compose operations.
Confusion 4: async Does Not Mean Parallel
Starting all tasks before awaiting them enables parallel execution. Task.WhenAll (C#) and Promise.all (JS) wait for all tasks to complete.
JavaScript-Specific Confusions
Common Pitfalls
- Using
.Resultor.Wait()on async methods: These block the calling thread and cause deadlocks in ASP.NET, WPF, and WinForms due to theSynchronizationContext. Useawaitinstead. If you must call async from sync code, useTask.Run(() => method()).GetAwaiter().GetResult()as a last resort. - Using
async voidinstead ofasync Task:async voidmethods cannot be awaited, and their exceptions crash the application. Only useasync voidfor event handlers. All other async methods should returnTaskorTask<T>. - Awaiting inside
forEachexpecting sequential execution: In JavaScript,Array.forEachdoes not await async callbacks — all iterations start concurrently. Usefor...offor sequential execution orPromise.all(array.map(...))for intentional parallelism. - Making everything async unnecessarily: Adding
async/awaitto a method that just returns another task adds overhead from the state machine. If a method only calls one async method and returns its result, return the task directly:return FetchDataAsync()instead ofreturn await FetchDataAsync(). - Not handling exceptions from
Task.WhenAll:Task.WhenAllthrows only the first exception. Other exceptions are silently swallowed unless you inspect each task's.Exceptionproperty individually. Always check all task results afterWhenAll.
Summary
asyncdoes not create threads — it enables non-blocking waits withawait- Always await async calls or explicitly discard with
_ = MethodAsync() - Never use
.Resultor.Wait()— they cause deadlocks in UI and web contexts - Return
TaskorTask<T>, neverasync void(except for event handlers) - Start multiple tasks before awaiting for parallel execution with
Task.WhenAll - In JavaScript, use
for...of(notforEach) for sequential async iteration
Related reading
- Async multiprocessing python
- Async not working for method having return type void
- Async not working in Spring API rest with Interfaces
- Async operations with I/O Completion Ports return 0 bytes transferred
- Async pass multiple parameters into ReportProgress method
- Async PHP Processing data into several systems Advice
- Async play sound in javascript?
- Async ProcessStartInfo Run cmd program to show in textbox just like cmd window in real time
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.