Created
September 30, 2017 06:36
-
-
Save gyuwon/5e06b82bda1cf30432e58da5a2180e6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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