public function then($onComplete, $onError = null) { if (!is_callable($onComplete)) { $onComplete = null; } if (!is_callable($onError)) { $onError = null; } if ($onComplete !== null || $onError !== null) { if ($onComplete !== null && $this->status === Future::FULFILLED) { $x = $this->result; if ($x instanceof Future) { if ($x === $this) { throw new \Exception('Self resolution'); } return $x->then($onComplete, $onError); } return Future::create($onComplete, $x); } if ($onError !== null && $this->status === Future::REJECTED) { return Future::create($onError, $this->error); } $this->callbacks[] = array($onComplete, $onError); } return $this; }
public function completeError($error) { if ($this->future->status === Future::PENDING) { $this->future->status = Future::REJECTED; $this->future->error = $error; while (count($this->future->callbacks) > 0) { $callback = array_shift($this->future->callbacks); if ($callback[1] !== null) { $result = Future::create($callback[1], $error); break; } } if (count($this->future->callbacks) > 0) { do { $callback = array_shift($this->future->callbacks); $result = $this->resolve($callback[0], $callback[1], $result); } while (count($this->future->callbacks) > 0); } } }