I have awaited only Task and Task of TResult but everything which has GetAwaiter() method can be awaited (either instance method - Awaitable pattern or extension method).
Has anyone await something different than Task it in real cases? If yes why?
Examples
public class MyAwaitableClass
{
public MyAwaiter GetAwaiter()
{
return new MyAwaiter();
}
}
public class MyAwaiter : INotifyCompletion
{
public void GetResult()
{
}
public bool IsCompleted
{
get { return false; }
}
//From INotifyCompletion
public void OnCompleted(Action continuation)
{
}
}
public static class TimeSpanExtensions
{
public static TaskAwaiter GetAwaiter(this TimeSpan timeSpan)
{
return TaskEx.Delay(timeSpan).GetAwaiter();
}
}
usage:
MyAwaitableClass awaitableObject = new MyAwaitableClass();
await awaitableObject;
await TimeSpan.FromSeconds(15);
Regards