wrappedPromise = $promise; $this->waitfn = $wait; $this->cancelfn = $cancel; } /** * @return mixed */ public function wait() { if (!$this->isRealized) { $this->addShadow(); if (!$this->isRealized && $this->waitfn) { $this->invokeWait(); } if (!$this->isRealized) { $this->error = new RingException('Waiting did not resolve future'); } } if ($this->error) { throw $this->error; } return $this->result; } /** * @return PromiseInterface */ public function promise() { return $this->wrappedPromise; } /** * @return PromiseInterface */ public function then( callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null ) { return $this->wrappedPromise->then($onFulfilled, $onRejected, $onProgress); } public function cancel() { if (!$this->isRealized) { $cancelfn = $this->cancelfn; $this->waitfn = $this->cancelfn = null; $this->isRealized = true; $this->error = new CancelledFutureAccessException(); if ($cancelfn) { $cancelfn($this); } } } private function addShadow() { // Get the result and error when the promise is resolved. Note that // calling this function might trigger the resolution immediately. $this->wrappedPromise->then( function ($value) { $this->isRealized = true; $this->result = $value; $this->waitfn = $this->cancelfn = null; }, function ($error) { $this->isRealized = true; $this->error = $error; $this->waitfn = $this->cancelfn = null; } ); } private function invokeWait() { try { $wait = $this->waitfn; $this->waitfn = null; $wait(); } catch (\Exception $e) { // Defer can throw to reject. $this->error = $e; $this->isRealized = true; } } }