Esempio n. 1
0
 public static Task <ResultEx <TResult> > ThenAsync <TValue, TResult>(this ResultEx <TValue> result, Func <TValue, Task <TResult> > func)
 {
     return(result.IsError
         ? Task.FromResult(ResultEx <TResult> .FromError(result.Error))
         : func(result.Value).ContinueWith(task => ResultEx <TResult> .FromValue(task.Result), CancellationToken.None,
                                           TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default));
 }
Esempio n. 2
0
 public static Task <ResultEx <T> > Where <T>(this Task <ResultEx <T> > resultTask, Func <T, bool> predicate,
                                              Func <T, string> errorMessageFunc, TaskScheduler scheduler = null)
 {
     return(resultTask.ContinueWith((task, f) =>
     {
         var result = task.Result;
         return result.IsError ? ResultEx <T> .FromError(result.Error) : result.Where((Func <T, bool>)f, errorMessageFunc);
     }, predicate, CancellationToken.None, TaskContinuationOptions.DenyChildAttach, scheduler ?? TaskScheduler.Default));
 }
Esempio n. 3
0
 public static Task <ResultEx <TResult> > ThenAsync <TValue, TResult>(this Task <ResultEx <TValue> > resultTask,
                                                                      Func <TValue, Task <ResultEx <TResult> > > func, TaskScheduler scheduler = null)
 {
     return(resultTask.ContinueWith((task, f) =>
     {
         return task.Result.IsError
                 ? Task.FromResult(ResultEx <TResult> .FromError(task.Result.Error))
                 : ((Func <TValue, Task <ResultEx <TResult> > >)f)(task.Result.Value)
         .ContinueWith(t => t.Result, TaskContinuationOptions.ExecuteSynchronously);
     }, func,
                                    CancellationToken.None, TaskContinuationOptions.DenyChildAttach,
                                    scheduler ?? TaskScheduler.Default).Unwrap());
 }
        public static Task <ResultEx <TResult> > ThenTryAsync <TValue, TResult>(this Result <TValue> result,
                                                                                Func <TValue, Task <Result <TResult> > > func)
        {
            if (result.IsError)
            {
                return(Task.FromResult(ResultEx <TResult> .FromError(new ResultErrorException(result.Error))));
            }

            try
            {
                return(func(result.Value).ContinueWith(task => task.Result.ToResultEx(),
                                                       TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously));
            }
            catch (Exception e)
            {
                return(Task.FromResult(ResultEx <TResult> .FromError(e)));
            }
        }
        public static Task <ResultEx <TResult> > ThenTryAsync <TValue, TResult>(this Result <TValue> result, Func <TValue, Task <TResult> > func)
        {
            if (result.IsError)
            {
                return(Task.FromResult(ResultEx <TResult> .FromError(new ResultErrorException(result.Error))));
            }

            try
            {
                return(func(result.Value)
                       .ContinueWith(task => task.IsFaulted
                            ? ResultEx <TResult> .FromError(task.Exception.TryFlattenAggregateException())
                            : ResultEx <TResult> .FromValue(
                                         task.Result), CancellationToken.None,
                                     TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default));
            }
            catch (Exception e)
            {
                var exception = (e as AggregateException)?.GetBaseException() ?? e;
                return(Task.FromResult(ResultEx <TResult> .FromError(exception)));
            }
        }
        public static Task <ResultEx <TResult> > ThenTryAsync <TValue, TResult>(this Task <Result <TValue> > resultTask,
                                                                                Func <TValue, Task <TResult> > func, TaskScheduler scheduler = null)
        {
            return(resultTask.ContinueWith((task, f) =>
            {
                var result = task.Result;
                if (result.IsError)
                {
                    return Task.FromResult(ResultEx <TResult> .FromError(new ResultErrorException(result.Error)));
                }

                try
                {
                    return ((Func <TValue, Task <TResult> >)f)(task.Result.Value)
                    .ContinueWith(t => ResultEx.FromValue(t.Result), TaskContinuationOptions.ExecuteSynchronously);
                }
                catch (Exception e)
                {
                    return Task.FromResult(ResultEx <TResult> .FromError(e));
                }
            }, func, CancellationToken.None, TaskContinuationOptions.DenyChildAttach,
                                           scheduler ?? TaskScheduler.Default).Unwrap());
        }
Esempio n. 7
0
 public static Task <ResultEx <TResult> > ThenAsync <TValue, TResult>(this ResultEx <TValue> result, Func <TValue, Task <ResultEx <TResult> > > func)
 {
     return(result.IsError
         ? Task.FromResult(ResultEx <TResult> .FromError(result.Error))
         : func(result.Value));
 }