PromiseInterface.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Promise;
  4. /**
  5. * A promise represents the eventual result of an asynchronous operation.
  6. *
  7. * The primary way of interacting with a promise is through its then method,
  8. * which registers callbacks to receive either a promise’s eventual value or
  9. * the reason why the promise cannot be fulfilled.
  10. *
  11. * @see https://promisesaplus.com/
  12. */
  13. interface PromiseInterface
  14. {
  15. public const PENDING = 'pending';
  16. public const FULFILLED = 'fulfilled';
  17. public const REJECTED = 'rejected';
  18. /**
  19. * Appends fulfillment and rejection handlers to the promise, and returns
  20. * a new promise resolving to the return value of the called handler.
  21. *
  22. * @param callable $onFulfilled Invoked when the promise fulfills.
  23. * @param callable $onRejected Invoked when the promise is rejected.
  24. */
  25. public function then(
  26. callable $onFulfilled = null,
  27. callable $onRejected = null
  28. ): PromiseInterface;
  29. /**
  30. * Appends a rejection handler callback to the promise, and returns a new
  31. * promise resolving to the return value of the callback if it is called,
  32. * or to its original fulfillment value if the promise is instead
  33. * fulfilled.
  34. *
  35. * @param callable $onRejected Invoked when the promise is rejected.
  36. */
  37. public function otherwise(callable $onRejected): PromiseInterface;
  38. /**
  39. * Get the state of the promise ("pending", "rejected", or "fulfilled").
  40. *
  41. * The three states can be checked against the constants defined on
  42. * PromiseInterface: PENDING, FULFILLED, and REJECTED.
  43. */
  44. public function getState(): string;
  45. /**
  46. * Resolve the promise with the given value.
  47. *
  48. * @param mixed $value
  49. *
  50. * @throws \RuntimeException if the promise is already resolved.
  51. */
  52. public function resolve($value): void;
  53. /**
  54. * Reject the promise with the given reason.
  55. *
  56. * @param mixed $reason
  57. *
  58. * @throws \RuntimeException if the promise is already resolved.
  59. */
  60. public function reject($reason): void;
  61. /**
  62. * Cancels the promise if possible.
  63. *
  64. * @see https://github.com/promises-aplus/cancellation-spec/issues/7
  65. */
  66. public function cancel(): void;
  67. /**
  68. * Waits until the promise completes if possible.
  69. *
  70. * Pass $unwrap as true to unwrap the result of the promise, either
  71. * returning the resolved value or throwing the rejected exception.
  72. *
  73. * If the promise cannot be waited on, then the promise will be rejected.
  74. *
  75. * @return mixed
  76. *
  77. * @throws \LogicException if the promise has no wait function or if the
  78. * promise does not settle after waiting.
  79. */
  80. public function wait(bool $unwrap = true);
  81. }