1
0
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:
Aaron Piotrowski 2017-06-09 10:18:06 -05:00
parent f83d36dfe5
commit 9757b39fab

View File

@ -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);