JS
C#

JavaScript to C#

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Async/Await4Array Methods → LINQ5Exception Handling6Collections7Generics8Delegates and Events9Records and Pattern Matching10File I/O
All Mirror Courses
JS
C#
Async/Await
MirrorLesson 3 of 10
Lesson 3

Async/Await

Asynchronous programming

Introduction

In this lesson, you'll learn about async/await in C#. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with asynchronous programming.

C#
In C#:

C# has its own approach to asynchronous programming, which we'll explore step by step.

The C# Way

Let's see how C# handles this concept. Here's a typical example:

C#
C# Example
using System.Net.Http;
using System.Threading.Tasks;

async Task<User> FetchUser(int id) {
  using var client = new HttpClient();
  var res = await client.GetAsync("/api/users/" + id);
  res.EnsureSuccessStatusCode();
  return await res.Content.ReadFromJsonAsync<User>();
}

async Task Main() {
  try {
    var user = await FetchUser(1);
    Console.WriteLine(user.Name);
  } catch (HttpRequestException ex) {
    Console.Error.WriteLine(ex.Message);
  }
}

// Parallel
var (a, b) = await Task.WhenAll(FetchUser(1), FetchUser(2));

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
async function fetchUser(id) {
  const res = await fetch("/api/users/" + id);
  if (!res.ok) throw new Error("HTTP " + res.status);
  return res.json();
}

async function main() {
  try {
    const user = await fetchUser(1);
    console.log(user.name);
  } catch (err) {
    console.error(err);
  }
}

// Parallel
const [a, b] = await Promise.all([fetchUser(1), fetchUser(2)]);
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# async/await is nearly identical to JS async/await

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# returns Task<T> instead of JS Promise<T>

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

Task.WhenAll replaces Promise.all

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# async methods conventionally end with 'Async' suffix

Step-by-Step Breakdown

1. Task vs Promise

C# Task<T> is the equivalent of JS Promise<T>. async/await syntax is nearly identical in both languages.

JS
JavaScript
async function getUser(): Promise<User>
C#
C#
async Task<User> GetUserAsync()

2. Task.WhenAll

Task.WhenAll runs multiple async operations in parallel, just like Promise.all.

JS
JavaScript
const [a, b] = await Promise.all([f1(), f2()]);
C#
C#
await Task.WhenAll(task1, task2);
var a = task1.Result; var b = task2.Result;

3. Naming Convention

By convention, C# async methods end with Async (FetchUserAsync). This helps distinguish async from sync overloads.

Rule of Thumb
Always name async methods with the Async suffix in C#.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • C# async/await is nearly identical to JS async/await
  • C# returns Task<T> instead of JS Promise<T>
  • Task.WhenAll replaces Promise.all
Common Pitfall
Don't assume C# works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • C# async/await mirrors JS — nearly identical syntax
  • Promise<T> → Task<T>
  • Promise.all → Task.WhenAll
  • C# async methods end with Async by convention
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in C# to practice these concepts.
PreviousNext