function Promise::call
Parameters
callable(callable(mixed):void,callable(\Throwable):void):void $cb:
2 calls to Promise::call()
- Promise::cancel in vendor/
react/ promise/ src/ Promise.php - The `cancel()` method notifies the creator of the promise that there is no further interest in the results of the operation.
- Promise::__construct in vendor/
react/ promise/ src/ Promise.php
File
-
vendor/
react/ promise/ src/ Promise.php, line 247
Class
- Promise
- @template T @template-implements PromiseInterface<T>
Namespace
React\PromiseCode
private function call(callable $cb) : void {
// Explicitly overwrite argument with null value. This ensure that this
// argument does not show up in the stack trace in PHP 7+ only.
$callback = $cb;
$cb = null;
// Use reflection to inspect number of arguments expected by this callback.
// We did some careful benchmarking here: Using reflection to avoid unneeded
// function arguments is actually faster than blindly passing them.
// Also, this helps avoiding unnecessary function arguments in the call stack
// if the callback creates an Exception (creating garbage cycles).
if (\is_array($callback)) {
$ref = new \ReflectionMethod($callback[0], $callback[1]);
}
elseif (\is_object($callback) && !$callback instanceof \Closure) {
$ref = new \ReflectionMethod($callback, '__invoke');
}
else {
assert($callback instanceof \Closure || \is_string($callback));
$ref = new \ReflectionFunction($callback);
}
$args = $ref->getNumberOfParameters();
try {
if ($args === 0) {
$callback();
}
else {
// Keep references to this promise instance for the static resolve/reject functions.
// By using static callbacks that are not bound to this instance
// and passing the target promise instance by reference, we can
// still execute its resolving logic and still clear this
// reference when settling the promise. This helps avoiding
// garbage cycles if any callback creates an Exception.
// These assumptions are covered by the test suite, so if you ever feel like
// refactoring this, go ahead, any alternative suggestions are welcome!
$target =& $this;
$callback(static function ($value) use (&$target) : void {
if ($target !== null) {
$target->settle(resolve($value));
$target = null;
}
}, static function (\Throwable $reason) use (&$target) : void {
if ($target !== null) {
$target->reject($reason);
$target = null;
}
});
}
} catch (\Throwable $e) {
$target = null;
$this->reject($e);
}
}