diff --git a/lib/LazyPromise.php b/lib/LazyPromise.php index 61ead25..652b77e 100644 --- a/lib/LazyPromise.php +++ b/lib/LazyPromise.php @@ -2,14 +2,12 @@ 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 * 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 */ private $promisor; @@ -17,7 +15,8 @@ class LazyPromise implements 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) { $this->promisor = $promisor; @@ -30,20 +29,7 @@ class LazyPromise implements Promise { if ($this->promise === null) { $provider = $this->promisor; $this->promisor = null; - - 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 = call($provider); } $this->promise->onResolve($onResolved);