Handling Async Operations Lite

Provides essential async programming patterns with Task and ValueTask. Use when quickly referencing core async/await techniques without detailed explanations.

majiayu000 Updated 567 repo stars

File contents

Async Programming Essentials

1. Task vs ValueTask

// General async: Use Task
public async Task<Data> LoadAsync() { }

// Frequent cache hits: Use ValueTask
public ValueTask<Data> GetAsync(string key)
{
    if (_cache.TryGetValue(key, out var cached))
        return new ValueTask<Data>(cached);

    return new ValueTask<Data>(LoadFromDbAsync(key));
}

2. CancellationToken

public async Task<Data> LoadAsync(CancellationToken ct = default)
{
    ct.ThrowIfCancellationRequested();
    return await _httpClient.GetFromJsonAsync<Data>(url, ct);
}

// Timeout
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
await LongOperationAsync(cts.Token);

3. Anti-patterns

// ❌ No async void
public async void BadMethod() { }

// ❌ No .Result, .Wait() (deadlock)
var result = GetDataAsync().Result;

// ✅ Use await
var result = await GetDataAsync();

For details: See /dotnet-async skill

majiayu000/claude-skill-registry-data/tree/main/development/handling-async-operations-lite commit c9b1977a13

Frequently asked questions

npx skillmds@latest add majiayu000/handling-async-operations-lite