DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="3.1.1" />

 CompletedTask<T, TConstant>

public static class CompletedTask<T, TConstant> where TConstant : Constant<T>
Represents cache of completed tasks.
using DotNext.Generic; using DotNext.Runtime; using System; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace DotNext.Threading.Tasks { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class CompletedTask<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TConstant> where TConstant : Constant<T>, new { private static readonly T Value = (Constant<T>)new TConstant(); public static readonly Task<T> Task = System.Threading.Tasks.Task.FromResult<T>(Value); [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool IsFaulted(Task<T> task, [System.Runtime.CompilerServices.Nullable(2)] object predicate) { if (task.IsFaulted) return Intrinsics.Cast<Predicate<AggregateException>>(predicate)(task.Exception); return false; } internal static T WhenFaulted(Task<T> task, [System.Runtime.CompilerServices.Nullable(2)] object predicate) { if (!IsFaulted(task, predicate)) return task.GetAwaiter().GetResult(); return Value; } internal static T WhenCanceled(Task<T> task) { if (!task.IsCanceled) return task.GetAwaiter().GetResult(); return Value; } internal static T WhenFaultedOrCanceled(Task<T> task, [System.Runtime.CompilerServices.Nullable(2)] object predicate) { if (!IsFaulted(task, predicate) && !task.IsCanceled) return task.GetAwaiter().GetResult(); return Value; } } }