| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Promise; |
| 4 |
|
| 5 |
class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface |
| 6 |
{ |
| 7 |
private $factory; |
| 8 |
private $promise; |
| 9 |
|
| 10 |
public function __construct(callable $factory) |
| 11 |
{ |
| 12 |
$this->factory = $factory; |
| 13 |
} |
| 14 |
|
| 15 |
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
| 16 |
{ |
| 17 |
return $this->promise()->then($onFulfilled, $onRejected, $onProgress); |
| 18 |
} |
| 19 |
|
| 20 |
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
| 21 |
{ |
| 22 |
return $this->promise()->done($onFulfilled, $onRejected, $onProgress); |
| 23 |
} |
| 24 |
|
| 25 |
public function otherwise(callable $onRejected) |
| 26 |
{ |
| 27 |
return $this->promise()->otherwise($onRejected); |
| 28 |
} |
| 29 |
|
| 30 |
public function always(callable $onFulfilledOrRejected) |
| 31 |
{ |
| 32 |
return $this->promise()->always($onFulfilledOrRejected); |
| 33 |
} |
| 34 |
|
| 35 |
public function progress(callable $onProgress) |
| 36 |
{ |
| 37 |
return $this->promise()->progress($onProgress); |
| 38 |
} |
| 39 |
|
| 40 |
public function cancel() |
| 41 |
{ |
| 42 |
return $this->promise()->cancel(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @internal |
| 47 |
* @see Promise::settle() |
| 48 |
*/ |
| 49 |
public function promise() |
| 50 |
{ |
| 51 |
if (null === $this->promise) { |
| 52 |
try { |
| 53 |
$this->promise = resolve(\call_user_func($this->factory)); |
| 54 |
} catch (\Throwable $exception) { |
| 55 |
$this->promise = new RejectedPromise($exception); |
| 56 |
} catch (\Exception $exception) { |
| 57 |
$this->promise = new RejectedPromise($exception); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return $this->promise; |
| 62 |
} |
| 63 |
} |
| 64 |
|