---
title: "How would you implement a custom awaitable object?"  
description: "How would you implement a custom awaitable object?"  
author: "ICSM Computer"  
published: 2025-06-16  
updated: 2025-06-18  
canonical: https://www.mindstick.com/forum/161716/how-would-you-implement-a-custom-awaitable-object  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How would you implement a custom awaitable object?

How would you implement a [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) awaitable object?

## Replies

### Reply by Anubhav Sharma

To implement a **custom awaitable object** in C#, you need to create a type that can be used with the `await` keyword. This requires understanding how the C# compiler translates `await` into a series of method calls.

## High-Level Summary

To be **awaitable**, your custom type must:

- Have a method called `GetAwaiter()`.
- That method returns an **awaiter** object which must:

   - Implement `INotifyCompletion` or `ICriticalNotifyCompletion`
   - Have a `bool IsCompleted { get; }` property
   - Have a `void OnCompleted(Action)` method
   - Have a `T GetResult()` method (returns result after awaiting)

## Components of a Custom Awaitable

### 1. Awaitable Object

This is the object you call `await` on.

### 2. Awaiter Object

Returned by `GetAwaiter()`; it contains logic for managing the async wait.

## Example: Simple Custom Awaitable

Let’s create a custom awaitable that just delays and returns a message.

## Custom Awaitable

```cs
public class MyCustomAwaitable
{
    private readonly int _delay;

    public MyCustomAwaitable(int delay)
    {
        _delay = delay;
    }

    public MyCustomAwaiter GetAwaiter()
    {
        return new MyCustomAwaiter(_delay);
    }
}
```

## Custom Awaiter

```cs
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

public class MyCustomAwaiter : INotifyCompletion
{
    private readonly int _delay;
    private Task _task;

    public MyCustomAwaiter(int delay)
    {
        _delay = delay;
        _task = Task.Delay(delay); // simulate async operation
    }

    public bool IsCompleted => _task.IsCompleted;

    public void OnCompleted(Action continuation)
    {
        _task.ContinueWith(t => continuation());
    }

    public string GetResult()
    {
        // Can also throw exceptions or return results
        return $"Completed after {_delay}ms!";
    }
}
```

## Usage

```cs
public async Task UseCustomAwaitable()
{
    var result = await new MyCustomAwaitable(1000);
    Console.WriteLine(result);  // Output: Completed after 1000ms!
}
```

## Key Concepts

| Element | Purpose |
| --- | --- |
| `GetAwaiter()` | Returns the awaiter |
| `IsCompleted` | Indicates if the task is already done |
| `OnCompleted()` | Registers the continuation (what to do after await finishes) |
| `GetResult()` | Returns the result after await is done |

## Real-World Use Cases

- Custom throttling or retry logic
- Async wrapper around I/O
- Awaiting external signals (file system, messages, etc.)
- Simulation/test awaitables

Let me know if you want:

- A **generic version** (returning `T`)
- Integration with `CancellationToken`
- A **non-Task-based awaiter** (e.g., for events or custom I/O)


---

Original Source: https://www.mindstick.com/forum/161716/how-would-you-implement-a-custom-awaitable-object

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
