Wednesday, August 16, 2017

At times it may be wise to make a dummy Task in C# that doesn't do any asynchronous work at all as part of workflow satisfaction for conditional logic.

public static List<Anxiety> GetAnxieties(string phobia)
{
   Task<IEnumerable<Anxiety>> phobosAnxieties;
   if (phobia == null)
   {
      phobosAnxieties = Task.Run(() =>
      {
         Anxiety[] emptyArray = new Anxiety[] {};
         IEnumerable<Anxiety> emptyIEnumerable = emptyArray;
         return emptyIEnumerable;
      });
   }
   else
   {
      phobosAnxieties = Task.Run(() =>
         PhobosService.Call(phobia)
      );
   }
   List<Anxiety> anxieties = DeimosRepository.GetBaseDreads();
   foreach (var phobosAnxiety in phobosAnxieties.Result)
   {
      anxieties.Add(phobosAnxiety);
   }
   return anxieties;
}

No comments:

Post a Comment