function Promise::settle
Same name in this branch
- 11.1.x vendor/react/promise/src/Promise.php \React\Promise\Promise::settle()
2 calls to Promise::settle()
- Promise::reject in vendor/
guzzlehttp/ promises/ src/ Promise.php - Reject the promise with the given reason.
- Promise::resolve in vendor/
guzzlehttp/ promises/ src/ Promise.php - Resolve the promise with the given value.
File
-
vendor/
guzzlehttp/ promises/ src/ Promise.php, line 123
Class
- Promise
- Promises/A+ implementation that avoids recursion when possible.
Namespace
GuzzleHttp\PromiseCode
private function settle(string $state, $value) : void {
if ($this->state !== self::PENDING) {
// Ignore calls with the same resolution.
if ($state === $this->state && $value === $this->result) {
return;
}
throw $this->state === $state ? new \LogicException("The promise is already {$state}.") : new \LogicException("Cannot change a {$this->state} promise to {$state}");
}
if ($value === $this) {
throw new \LogicException('Cannot fulfill or reject a promise with itself');
}
// Clear out the state of the promise but stash the handlers.
$this->state = $state;
$this->result = $value;
$handlers = $this->handlers;
$this->handlers = null;
$this->waitList = $this->waitFn = null;
$this->cancelFn = null;
if (!$handlers) {
return;
}
// If the value was not a settled promise or a thenable, then resolve
// it in the task queue using the correct ID.
if (!is_object($value) || !method_exists($value, 'then')) {
$id = $state === self::FULFILLED ? 1 : 2;
// It's a success, so resolve the handlers in the queue.
Utils::queue()->add(static function () use ($id, $value, $handlers) : void {
foreach ($handlers as $handler) {
self::callHandler($id, $value, $handler);
}
});
}
elseif ($value instanceof Promise && Is::pending($value)) {
// We can just merge our handlers onto the next promise.
$value->handlers = array_merge($value->handlers, $handlers);
}
else {
// Resolve the handlers when the forwarded promise is resolved.
$value->then(static function ($value) use ($handlers) : void {
foreach ($handlers as $handler) {
self::callHandler(1, $value, $handler);
}
}, static function ($reason) use ($handlers) : void {
foreach ($handlers as $handler) {
self::callHandler(2, $reason, $handler);
}
});
}
}