Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created August 24, 2017 21:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/722fa02a65c07b549781d421d1a88d26 to your computer and use it in GitHub Desktop.
Save dcomartin/722fa02a65c07b549781d421d1a88d26 to your computer and use it in GitHub Desktop.
/// <summary>
/// Provides support for asynchronous lazy initialization.
/// </summary>
/// <typeparam name="T"></typeparam>
public class LazyAsync<T> : Lazy<Task<T>>
{
/// <summary>
/// Initializes a new instance of the LazyAsync`1 class. When lazy initialization
/// occurs, the specified initialization function is used.
/// </summary>
/// <param name="taskFunc">The delegate that is invoked to produce the lazily initialized Task when it is needed.</param>
public LazyAsync(Func<Task<T>> taskFunc) :
base(() => Task.Factory.StartNew(taskFunc).Unwrap()) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment