Skip to content

Instantly share code, notes, and snippets.

@gyuwon
Created September 30, 2017 06:36
Show Gist options
  • Save gyuwon/5e06b82bda1cf30432e58da5a2180e6f to your computer and use it in GitHub Desktop.
Save gyuwon/5e06b82bda1cf30432e58da5a2180e6f to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
namespace FailFastAsync
{
public class Program
{
public static async Task Main(string[] args)
{
var functions = new Functions();
Console.WriteLine(await functions.FailFastAsyncUsingAnonymousFunction("Anonymous Function"));
Console.WriteLine(await functions.FailFastAsyncUsingLocalFunction("Local Function"));
Console.WriteLine(await functions.FailFastAsyncUsingMethod("Method"));
}
}
public class Functions
{
public Task<string> FailFastAsyncUsingAnonymousFunction(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
Func<Task<string>> core = async () =>
{
await Task.Delay(10);
return $"Hello {name}";
};
return core();
}
public Task<string> FailFastAsyncUsingLocalFunction(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
async Task<string> core()
{
await Task.Delay(10);
return $"Hello {name}";
};
return core();
}
public Task<string> FailFastAsyncUsingMethod(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return FailFastAsyncUsingMethodCore(name);
}
private async Task<string> FailFastAsyncUsingMethodCore(string name)
{
await Task.Delay(10);
return $"Hello {name}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment