mirror of
https://github.com/danog/amp.git
synced 2025-01-22 13:21:16 +01:00
Use call in LazyPromise and make class final
This commit is contained in:
parent
f83d36dfe5
commit
9757b39fab
@ -2,14 +2,12 @@
|
|||||||
|
|
||||||
namespace Amp;
|
namespace Amp;
|
||||||
|
|
||||||
use React\Promise\PromiseInterface as ReactPromise;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a promise that calls $promisor only when the result of the promise is requested (i.e. onResolve() is called
|
* Creates a promise that calls $promisor only when the result of the promise is requested (i.e. onResolve() is called
|
||||||
* on the promise). $promisor can return a promise or any value. If $promisor throws an exception, the promise fails
|
* on the promise). $promisor can return a promise or any value. If $promisor throws an exception, the promise fails
|
||||||
* with that exception.
|
* with that exception. If $promisor returns a Generator, it will be run as a coroutine.
|
||||||
*/
|
*/
|
||||||
class LazyPromise implements Promise {
|
final class LazyPromise implements Promise {
|
||||||
/** @var callable|null */
|
/** @var callable|null */
|
||||||
private $promisor;
|
private $promisor;
|
||||||
|
|
||||||
@ -17,7 +15,8 @@ class LazyPromise implements Promise {
|
|||||||
private $promise;
|
private $promise;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param callable $promisor Function which starts an async operation, returning a Promise or any value.
|
* @param callable $promisor Function which starts an async operation, returning a Promise (or any value).
|
||||||
|
* Generators will be run as a coroutine.
|
||||||
*/
|
*/
|
||||||
public function __construct(callable $promisor) {
|
public function __construct(callable $promisor) {
|
||||||
$this->promisor = $promisor;
|
$this->promisor = $promisor;
|
||||||
@ -30,20 +29,7 @@ class LazyPromise implements Promise {
|
|||||||
if ($this->promise === null) {
|
if ($this->promise === null) {
|
||||||
$provider = $this->promisor;
|
$provider = $this->promisor;
|
||||||
$this->promisor = null;
|
$this->promisor = null;
|
||||||
|
$this->promise = call($provider);
|
||||||
try {
|
|
||||||
$this->promise = $provider();
|
|
||||||
|
|
||||||
if ($this->promise instanceof \Generator) {
|
|
||||||
$this->promise = new Coroutine($this->promise);
|
|
||||||
} elseif ($this->promise instanceof ReactPromise) {
|
|
||||||
$this->promise = Promise\adapt($this->promise);
|
|
||||||
} elseif (!$this->promise instanceof Promise) {
|
|
||||||
$this->promise = new Success($this->promise);
|
|
||||||
}
|
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
$this->promise = new Failure($exception);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->promise->onResolve($onResolved);
|
$this->promise->onResolve($onResolved);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user