You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/asynchronous-programming/index.md
+30Lines changed: 30 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -263,6 +263,36 @@ The code completes the asynchronous breakfast tasks in about 15 minutes. The tot
263
263
264
264
The final code is asynchronous. It more accurately reflects how a person might cook breakfast. Compare the final code with the first code sample in the article. The core actions are still clear by reading the code. You can read the final code the same way you read the list of instructions for making a breakfast, as shown at the beginning of the article. The language features for the `async` and `await` keywords provide the translation every person makes to follow the written instructions: Start tasks as you can and don't block while waiting for tasks to complete.
265
265
266
+
## Async/await vs ContinueWith
267
+
268
+
The `async` and `await` keywords provide syntactic simplification over using <xref:System.Threading.Tasks.Task.ContinueWith%2A?displayProperty=nameWithType> directly. While `async`/`await` and `ContinueWith` have similar semantics for handling asynchronous operations, the compiler doesn't necessarily translate `await` expressions directly into `ContinueWith` method calls. Instead, the compiler generates optimized state machine code that provides the same logical behavior. This transformation provides significant readability and maintainability benefits, especially when chaining multiple asynchronous operations.
269
+
270
+
Consider a scenario where you need to perform multiple sequential asynchronous operations. Here's how the same logic looks when implemented with `ContinueWith` compared to `async`/`await`:
271
+
272
+
### Using ContinueWith
273
+
274
+
With `ContinueWith`, each step in a sequence of asynchronous operations requires nested continuations:
The `async`/`await` approach offers several advantages:
287
+
288
+
-**Readability**: The code reads like synchronous code, making it easier to understand the flow of operations.
289
+
-**Maintainability**: Adding or removing steps in the sequence requires minimal code changes.
290
+
-**Error handling**: Exception handling with `try`/`catch` blocks works naturally, whereas `ContinueWith` requires careful handling of faulted tasks.
291
+
-**Debugging**: The call stack and debugger experience is much better with `async`/`await`.
292
+
-**Performance**: The compiler optimizations for `async`/`await` are more sophisticated than manual `ContinueWith` chains.
293
+
294
+
The benefit becomes even more apparent as the number of chained operations increases. While a single continuation might be manageable with `ContinueWith`, sequences of 3-4 or more asynchronous operations quickly become difficult to read and maintain. This pattern, known as "monadic do-notation" in functional programming, allows you to compose multiple asynchronous operations in a sequential, readable manner.
0 commit comments