Promise.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Http\Promise;
  3. /**
  4. * Promise represents a value that may not be available yet, but will be resolved at some point in future.
  5. * It acts like a proxy to the actual value.
  6. *
  7. * This interface is an extension of the promises/a+ specification.
  8. *
  9. * @see https://promisesaplus.com/
  10. *
  11. * @author Joel Wurtz <joel.wurtz@gmail.com>
  12. * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
  13. */
  14. interface Promise
  15. {
  16. /**
  17. * Promise has not been fulfilled or rejected.
  18. */
  19. const PENDING = 'pending';
  20. /**
  21. * Promise has been fulfilled.
  22. */
  23. const FULFILLED = 'fulfilled';
  24. /**
  25. * Promise has been rejected.
  26. */
  27. const REJECTED = 'rejected';
  28. /**
  29. * Adds behavior for when the promise is resolved or rejected (response will be available, or error happens).
  30. *
  31. * If you do not care about one of the cases, you can set the corresponding callable to null
  32. * The callback will be called when the value arrived and never more than once.
  33. *
  34. * @param callable|null $onFulfilled called when a response will be available
  35. * @param callable|null $onRejected called when an exception occurs
  36. *
  37. * @return Promise a new resolved promise with value of the executed callback (onFulfilled / onRejected)
  38. */
  39. public function then(callable $onFulfilled = null, callable $onRejected = null);
  40. /**
  41. * Returns the state of the promise, one of PENDING, FULFILLED or REJECTED.
  42. *
  43. * @return string
  44. */
  45. public function getState();
  46. /**
  47. * Wait for the promise to be fulfilled or rejected.
  48. *
  49. * When this method returns, the request has been resolved and if callables have been
  50. * specified, the appropriate one has terminated.
  51. *
  52. * When $unwrap is true (the default), the response is returned, or the exception thrown
  53. * on failure. Otherwise, nothing is returned or thrown.
  54. *
  55. * @param bool $unwrap Whether to return resolved value / throw reason or not
  56. *
  57. * @return mixed Resolved value, null if $unwrap is set to false
  58. *
  59. * @throws \Exception the rejection reason if $unwrap is set to true and the request failed
  60. */
  61. public function wait($unwrap = true);
  62. }