コード例 #1
0
 public static Task <ResultGeneric <TResult, TError> > ThenAsync <TValue, TError, TResult>(this ResultGeneric <TValue, TError> result, Func <TValue, Task <TResult> > func)
 {
     return(result.IsError
         ? Task.FromResult(ResultGeneric <TResult, TError> .FromError(result.Error))
         : func(result.Value).ContinueWith(task => ResultGeneric <TResult, TError> .FromValue(task.Result), CancellationToken.None,
                                           TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default));
 }
コード例 #2
0
 public static Task <ResultGeneric <TResult, TError> > ThenAsync <TValue, TError, TResult>(this Task <ResultGeneric <TValue, TError> > resultTask,
                                                                                           Func <TValue, Task <TResult> > func, TaskScheduler scheduler = null)
 {
     return(resultTask.ContinueWith((task, f) =>
     {
         return task.Result.IsError
                 ? Task.FromResult(ResultGeneric <TResult, TError> .FromError(task.Result.Error))
                 : ((Func <TValue, Task <TResult> >)f)(task.Result.Value)
         .ContinueWith(t => ResultGeneric <TResult, TError> .FromValue(t.Result), TaskContinuationOptions.ExecuteSynchronously);
     }, func,
                                    CancellationToken.None, TaskContinuationOptions.DenyChildAttach,
                                    scheduler ?? TaskScheduler.Default).Unwrap());
 }
コード例 #3
0
        public static Task <ResultGeneric <TResult, TError> > ThenTryAsync <TValue, TError, TResult>(this ResultGeneric <TValue, TError> result, Func <TValue, Task <TResult> > func,
                                                                                                     Func <Exception, TError> catchFunc)
        {
            if (result.IsError)
            {
                return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(result.Error)));
            }

            try
            {
                return(func(result.Value).ContinueWith(task => task.IsFaulted
                        ? ResultGeneric <TResult, TError> .FromError(catchFunc(task.Exception.TryFlattenAggregateException()))
                        : ResultGeneric <TResult, TError> .FromValue(task.Result), CancellationToken.None,
                                                       TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default));
            }
            catch (Exception e)
            {
                return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(catchFunc(e))));
            }
        }