IPromise.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright (c) 2025 TerraByte Inc.
  2. //
  3. // Defines the public interface for a Promise, which represents the eventual
  4. // completion (or failure) of an asynchronous operation and its resulting value.
  5. // This decouples the consumer from the implementation.
  6. using System;
  7. namespace Terra.Arbitrator.Promises
  8. {
  9. public enum PromiseState { Pending, Resolved, Rejected }
  10. public interface IPromise<out TResult>
  11. {
  12. /// <summary>
  13. /// Attaches a callback that will execute when the promise is successfully resolved.
  14. /// </summary>
  15. IPromise<TResult> Then(Action<TResult> onResolved);
  16. /// <summary>
  17. /// Attaches a function that returns a new promise, allowing operations to be chained.
  18. /// The chain will wait until the returned promise is resolved before continuing.
  19. /// </summary>
  20. /// <returns>A new promise that resolves with the result of the chained promise.</returns>
  21. IPromise<TNewResult> Then<TNewResult>(Func<TResult, IPromise<TNewResult>> onResolved);
  22. /// <summary>
  23. /// Attaches a callback that will execute when the promise is rejected.
  24. /// </summary>
  25. IPromise<TResult> Catch(Action<Exception> onRejected);
  26. /// <summary>
  27. /// Attaches a callback that will execute when the promise is settled (either resolved or rejected).
  28. /// </summary>
  29. void Finally(Action onFinally);
  30. }
  31. }