Skip to content

Instantly share code, notes, and snippets.

@tabakerov
Created June 19, 2018 14:46
Show Gist options
  • Save tabakerov/49df8fef092e9d0c1dfae1ea898157d2 to your computer and use it in GitHub Desktop.
Save tabakerov/49df8fef092e9d0c1dfae1ea898157d2 to your computer and use it in GitHub Desktop.
Return, Bind, Map and Tap for async tasks
static class TaskExtensions
{
static Task<T> Return<T>(T task) => Task.FromResult(task);
static async Task<R> Bind<T, R>(this Task<T> task, Func<T, Task<R>> cont) =>
await cont(await task.ConfigureAwait(false)).ConfigureAwait(false);
static async Task<R> Map<T, R>(this Task<T> task, Func<T, R> map) =>
map(await task.ConfigureAwait(false));
static async Task<T> Tap<T>(this Task<T> task, Func<T, Task> action)
{
await action(await task);
return await task;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment