Skip to content

Commit 12dcf28

Browse files
authored
Merge pull request #171 from Kubo2/oo-deferred
Initialize Deferred's state in the constructor
2 parents ee56552 + c06be4d commit 12dcf28

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

src/Deferred.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,27 @@ final class Deferred implements PromisorInterface
77
private $promise;
88
private $resolveCallback;
99
private $rejectCallback;
10-
private $canceller;
1110

1211
public function __construct(callable $canceller = null)
1312
{
14-
$this->canceller = $canceller;
13+
$this->promise = new Promise(function ($resolve, $reject): void {
14+
$this->resolveCallback = $resolve;
15+
$this->rejectCallback = $reject;
16+
}, $canceller);
1517
}
1618

1719
public function promise(): PromiseInterface
1820
{
19-
if (null === $this->promise) {
20-
$canceller = $this->canceller;
21-
$this->canceller = null;
22-
23-
$this->promise = new Promise(function ($resolve, $reject): void {
24-
$this->resolveCallback = $resolve;
25-
$this->rejectCallback = $reject;
26-
}, $canceller);
27-
}
28-
2921
return $this->promise;
3022
}
3123

3224
public function resolve($value = null): void
3325
{
34-
$this->promise();
35-
3626
($this->resolveCallback)($value);
3727
}
3828

3929
public function reject(\Throwable $reason): void
4030
{
41-
$this->promise();
42-
4331
($this->rejectCallback)($reason);
4432
}
4533
}

0 commit comments

Comments
 (0)