PromiseInterface.php 2.8 KB

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