FulfilledPromise.php 994 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Http\Promise;
  3. /**
  4. * A promise already fulfilled.
  5. *
  6. * @author Joel Wurtz <joel.wurtz@gmail.com>
  7. */
  8. final class FulfilledPromise implements Promise
  9. {
  10. /**
  11. * @var mixed
  12. */
  13. private $result;
  14. /**
  15. * @param $result
  16. */
  17. public function __construct($result)
  18. {
  19. $this->result = $result;
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function then(callable $onFulfilled = null, callable $onRejected = null)
  25. {
  26. if (null === $onFulfilled) {
  27. return $this;
  28. }
  29. try {
  30. return new self($onFulfilled($this->result));
  31. } catch (\Exception $e) {
  32. return new RejectedPromise($e);
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getState()
  39. {
  40. return Promise::FULFILLED;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function wait($unwrap = true)
  46. {
  47. if ($unwrap) {
  48. return $this->result;
  49. }
  50. }
  51. }