mirror of
https://github.com/danog/amp.git
synced 2024-12-02 17:37:50 +01:00
Update promise docs and await()
This commit is contained in:
parent
b0b9489a2c
commit
99e765a8be
@ -11,15 +11,18 @@ namespace Amp;
|
|||||||
interface Promise
|
interface Promise
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Registers a callback to be invoked when the promise is resolved.
|
* Registers a callback to be invoked when the promise is resolved. Note that using this method directly is
|
||||||
|
* generally not recommended. Use the {@see await()} function to await promise resolution or use one of the
|
||||||
|
* combinator functions in the Amp\Promise namespace, such as {@see \Amp\Promise\all()}.
|
||||||
*
|
*
|
||||||
* If this method is called multiple times, additional handlers will be registered instead of replacing any already
|
* If this method is called multiple times, additional handlers will be registered instead of replacing any already
|
||||||
* existing handlers.
|
* existing handlers.
|
||||||
*
|
*
|
||||||
* If the promise is already resolved, the callback MUST be executed immediately.
|
* Registered callbacks MUST be invoked asynchronously when the promise is resolved using a defer watcher in the
|
||||||
|
* event-loop.
|
||||||
*
|
*
|
||||||
* Exceptions MUST NOT be thrown from this method. Any exceptions thrown from invoked callbacks MUST be
|
* Exceptions MUST NOT be thrown from this method. Any exceptions thrown from invoked callbacks MUST be
|
||||||
* forwarded to the event-loop error handler.
|
* forwarded to the event-loop error handler by re-throwing from a defer watcher.
|
||||||
*
|
*
|
||||||
* Note: You shouldn't implement this interface yourself. Instead, provide a method that returns a promise for the
|
* Note: You shouldn't implement this interface yourself. Instead, provide a method that returns a promise for the
|
||||||
* operation you're implementing. Objects other than pure placeholders implementing it are a very bad idea.
|
* operation you're implementing. Objects other than pure placeholders implementing it are a very bad idea.
|
||||||
@ -27,9 +30,7 @@ interface Promise
|
|||||||
* @param callable $onResolved The first argument shall be `null` on success, while the second shall be `null` on
|
* @param callable $onResolved The first argument shall be `null` on success, while the second shall be `null` on
|
||||||
* failure.
|
* failure.
|
||||||
*
|
*
|
||||||
* @psalm-param callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
* @psalm-param callable(\Throwable|null, mixed):Promise|null $onResolved
|
||||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
|
||||||
* mixed>|null) | callable(\Throwable|null, mixed): void $onResolved
|
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -30,13 +30,14 @@ namespace Amp
|
|||||||
$fiber = \Fiber::this();
|
$fiber = \Fiber::this();
|
||||||
$resolved = false;
|
$resolved = false;
|
||||||
|
|
||||||
if ($fiber) {
|
if ($fiber) { // Awaiting from within a fiber.
|
||||||
if (isset($loop) && $fiber === $loop) {
|
if ($fiber === $loop) {
|
||||||
throw new \Error(\sprintf('Cannot call %s() within an event loop callback', __FUNCTION__));
|
throw new \Error(\sprintf('Cannot call %s() within an event loop callback', __FUNCTION__));
|
||||||
}
|
}
|
||||||
|
|
||||||
$promise->onResolve(static function (?\Throwable $exception, mixed $value) use (&$resolved, $fiber): void {
|
$promise->onResolve(static function (?\Throwable $exception, mixed $value) use (&$resolved, $fiber): void {
|
||||||
$resolved = true;
|
$resolved = true;
|
||||||
|
|
||||||
if ($exception) {
|
if ($exception) {
|
||||||
$fiber->throw($exception);
|
$fiber->throw($exception);
|
||||||
return;
|
return;
|
||||||
@ -49,14 +50,16 @@ namespace Amp
|
|||||||
$value = \Fiber::suspend();
|
$value = \Fiber::suspend();
|
||||||
|
|
||||||
if (!$resolved) {
|
if (!$resolved) {
|
||||||
// $resolved should only be false if the function set in Promise::onResolve() did not resume the fiber.
|
// $resolved should only be false if the fiber was manually resumed outside of the callback above.
|
||||||
throw new \Error('Fiber resumed before promise was resolved');
|
throw new \Error('Fiber resumed before promise was resolved');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($loop) || $loop->isTerminated()) {
|
// Awaiting from {main}.
|
||||||
|
|
||||||
|
if (!$loop || $loop->isTerminated()) {
|
||||||
$loop = new \Fiber(static fn() => Loop::getDriver()->run());
|
$loop = new \Fiber(static fn() => Loop::getDriver()->run());
|
||||||
// Run event loop to completion on shutdown.
|
// Run event loop to completion on shutdown.
|
||||||
\register_shutdown_function(static function () use ($loop): void {
|
\register_shutdown_function(static function () use ($loop): void {
|
||||||
@ -68,26 +71,28 @@ namespace Amp
|
|||||||
|
|
||||||
$promise->onResolve(static function (?\Throwable $exception, mixed $value) use (&$resolved): void {
|
$promise->onResolve(static function (?\Throwable $exception, mixed $value) use (&$resolved): void {
|
||||||
$resolved = true;
|
$resolved = true;
|
||||||
|
|
||||||
// Suspend event loop fiber to {main}.
|
// Suspend event loop fiber to {main}.
|
||||||
\Fiber::suspend([$exception, $value]);
|
if ($exception) {
|
||||||
|
\Fiber::suspend(static fn() => throw $exception);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
\Fiber::suspend(static fn() => $value);
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
[$exception, $value] = $loop->isStarted() ? $loop->resume() : $loop->start();
|
$lambda = $loop->isStarted() ? $loop->resume() : $loop->start();
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
throw new \Error('Exception unexpectedly thrown from event loop', 0, $exception);
|
throw new \Error('Exception unexpectedly thrown from event loop', 0, $exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$resolved) {
|
if (!$resolved) {
|
||||||
// $resolved should only be false if the function set in Promise::onResolve() did not resume the fiber.
|
// $resolved should only be false if the event loop exited without resolving the promise.
|
||||||
throw new \Error('Event loop suspended or exited without resolving the promise');
|
throw new \Error('Event loop suspended or exited without resolving the promise');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($exception) {
|
return $lambda();
|
||||||
throw $exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user