IPromise.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. using UnityEngine.Scripting;
  8. namespace Terra.Arbitrator.Promises
  9. {
  10. public enum PromiseState { Pending, Resolved, Rejected }
  11. [Preserve]
  12. public interface IPromise<out TResult>
  13. {
  14. /// <summary>
  15. /// Attaches a callback that will execute when the promise is successfully resolved.
  16. /// </summary>
  17. IPromise<TResult> Then(Action<TResult> onResolved);
  18. /// <summary>
  19. /// Attaches a function that returns a new promise, allowing operations to be chained.
  20. /// The chain will wait until the returned promise is resolved before continuing.
  21. /// </summary>
  22. /// <returns>A new promise that resolves with the result of the chained promise.</returns>
  23. IPromise<TNewResult> Then<TNewResult>(Func<TResult, IPromise<TNewResult>> onResolved);
  24. /// <summary>
  25. /// Attaches a callback that will execute when the promise is rejected.
  26. /// </summary>
  27. IPromise<TResult> Catch(Action<Exception> onRejected);
  28. /// <summary>
  29. /// Attaches a callback that will execute when the promise is settled (either resolved or rejected).
  30. /// </summary>
  31. void Finally(Action onFinally);
  32. }
  33. }