public function resolve($value)
{
if ($value === $this) {
$this->reject(new TypeError('Self resolution'));
return;
}
if (isFuture($value)) {
$value->fill($this);
return;
}
if ($value !== NULL and is_object($value) or is_string($value)) {
if (method_exists($value, 'then')) {
$then = array($value, 'then');
$notrun = true;
$self = $this;
try {
call_user_func($then, function ($y) use(&$notrun, $self) {
if ($notrun) {
$notrun = false;
$self->resolve($y);
}
}, function ($r) use(&$notrun, $self) {
if ($notrun) {
$notrun = false;
$self->reject($r);
}
});
} catch (UncatchableException $e) {
throw $e->getPrevious();
} catch (Exception $e) {
if ($notrun) {
$notrun = false;
$this->reject($e);
}
} catch (Throwable $e) {
if ($notrun) {
$notrun = false;
$this->reject($e);
}
}
return;
}
}
if ($this->state === self::PENDING) {
$this->state = self::FULFILLED;
$this->value = $value;
while (count($this->subscribers) > 0) {
$subscriber = array_shift($this->subscribers);
$this->privateResolve($subscriber['onfulfill'], $subscriber['next'], $value);
}
}
}