PromiseInterface.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * @throws \RuntimeException if the promise is already resolved.
  55. */
  56. public function resolve($value);
  57. /**
  58. * Reject the promise with the given reason.
  59. *
  60. * @param mixed $reason
  61. * @throws \RuntimeException if the promise is already resolved.
  62. */
  63. public function reject($reason);
  64. /**
  65. * Cancels the promise if possible.
  66. *
  67. * @link https://github.com/promises-aplus/cancellation-spec/issues/7
  68. */
  69. public function cancel();
  70. /**
  71. * Waits until the promise completes if possible.
  72. *
  73. * Pass $unwrap as true to unwrap the result of the promise, either
  74. * returning the resolved value or throwing the rejected exception.
  75. *
  76. * If the promise cannot be waited on, then the promise will be rejected.
  77. *
  78. * @param bool $unwrap
  79. *
  80. * @return mixed
  81. * @throws \LogicException if the promise has no wait function or if the
  82. * promise does not settle after waiting.
  83. */
  84. public function wait($unwrap = true);
  85. }