Skip to content

Instantly share code, notes, and snippets.

@idiotandrobot
Created September 9, 2017 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idiotandrobot/781c9fc686c9868efcc64b0e3e8e3b75 to your computer and use it in GitHub Desktop.
Save idiotandrobot/781c9fc686c9868efcc64b0e3e8e3b75 to your computer and use it in GitHub Desktop.
AsyncLazy<T> v1
// https://blogs.msdn.microsoft.com/pfxteam/2011/01/15/asynclazyt/
public class AsyncLazy<T> : Lazy<Task<T>>
{
public AsyncLazy(Func<T> valueFactory) :
base(() => Task.Factory.StartNew(valueFactory)) { }
public AsyncLazy(Func<Task<T>> taskFactory) :
base(() => Task.Factory.StartNew(() => taskFactory()).Unwrap()) { }
public TaskAwaiter<T> GetAwaiter() { return Value.GetAwaiter(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment