GuzzleHttpPromiseCoroutine

Creates a promise that is resolved using a generator that yields values or promises (somewhat similar to C#'s async keyword).

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Promise/Coroutine.php  
  1. final class Coroutine implements PromiseInterface 
  2. /** 
  3. * @var PromiseInterface|null 
  4. */ 
  5. private $currentPromise; 
  6.  
  7. /** 
  8. * @var Generator 
  9. */ 
  10. private $generator; 
  11.  
  12. /** 
  13. * @var Promise 
  14. */ 
  15. private $result; 
  16.  
  17. public function __construct(callable $generatorFn) 
  18. $this->generator = $generatorFn(); 
  19. $this->result = new Promise(function () { 
  20. while (isset($this->currentPromise)) { 
  21. $this->currentPromise->wait(); 
  22. }); 
  23. $this->nextCoroutine($this->generator->current()); 
  24.  
  25. public function then( 
  26. callable $onFulfilled = null,  
  27. callable $onRejected = null 
  28. ) { 
  29. return $this->result->then($onFulfilled, $onRejected); 
  30.  
  31. public function otherwise(callable $onRejected) 
  32. return $this->result->otherwise($onRejected); 
  33.  
  34. public function wait($unwrap = true) 
  35. return $this->result->wait($unwrap); 
  36.  
  37. public function getState() 
  38. return $this->result->getState(); 
  39.  
  40. public function resolve($value) 
  41. $this->result->resolve($value); 
  42.  
  43. public function reject($reason) 
  44. $this->result->reject($reason); 
  45.  
  46. public function cancel() 
  47. $this->currentPromise->cancel(); 
  48. $this->result->cancel(); 
  49.  
  50. private function nextCoroutine($yielded) 
  51. $this->currentPromise = promise_for($yielded) 
  52. ->then([$this, '_handleSuccess'], [$this, '_handleFailure']); 
  53.  
  54. /** 
  55. * @internal 
  56. */ 
  57. public function _handleSuccess($value) 
  58. unset($this->currentPromise); 
  59. try { 
  60. $next = $this->generator->send($value); 
  61. if ($this->generator->valid()) { 
  62. $this->nextCoroutine($next); 
  63. } else { 
  64. $this->result->resolve($value); 
  65. } catch (Exception $exception) { 
  66. $this->result->reject($exception); 
  67. } catch (Throwable $throwable) { 
  68. $this->result->reject($throwable); 
  69.  
  70. /** 
  71. * @internal 
  72. */ 
  73. public function _handleFailure($reason) 
  74. unset($this->currentPromise); 
  75. try { 
  76. $nextYield = $this->generator->throw(exception_for($reason)); 
  77. // The throw was caught, so keep iterating on the coroutine 
  78. $this->nextCoroutine($nextYield); 
  79. } catch (Exception $exception) { 
  80. $this->result->reject($exception); 
  81. } catch (Throwable $throwable) { 
  82. $this->result->reject($throwable);