FulfilledPromise.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace React\Promise;
  3. /**
  4. * @deprecated 2.8.0 External usage of FulfilledPromise is deprecated, use `resolve()` instead.
  5. */
  6. class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
  7. {
  8. private $value;
  9. public function __construct($value = null)
  10. {
  11. if ($value instanceof PromiseInterface) {
  12. throw new \InvalidArgumentException('You cannot create React\Promise\FulfilledPromise with a promise. Use React\Promise\resolve($promiseOrValue) instead.');
  13. }
  14. $this->value = $value;
  15. }
  16. public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
  17. {
  18. if (null === $onFulfilled) {
  19. return $this;
  20. }
  21. try {
  22. return resolve($onFulfilled($this->value));
  23. } catch (\Throwable $exception) {
  24. return new RejectedPromise($exception);
  25. } catch (\Exception $exception) {
  26. return new RejectedPromise($exception);
  27. }
  28. }
  29. public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
  30. {
  31. if (null === $onFulfilled) {
  32. return;
  33. }
  34. $result = $onFulfilled($this->value);
  35. if ($result instanceof ExtendedPromiseInterface) {
  36. $result->done();
  37. }
  38. }
  39. public function otherwise(callable $onRejected)
  40. {
  41. return $this;
  42. }
  43. public function always(callable $onFulfilledOrRejected)
  44. {
  45. return $this->then(function ($value) use ($onFulfilledOrRejected) {
  46. return resolve($onFulfilledOrRejected())->then(function () use ($value) {
  47. return $value;
  48. });
  49. });
  50. }
  51. public function progress(callable $onProgress)
  52. {
  53. return $this;
  54. }
  55. public function cancel()
  56. {
  57. }
  58. }