Skip to content

Commit

Permalink
Refactor Promise to eliminate a Closure declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jul 17, 2015
1 parent ce79b9b commit 34f34af
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/Promise/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Promise implements PromiseInterface
private $onRejected;

/**
* @var \Closure|null
* @var callable|null
*/
private $onCancelled;

Expand Down Expand Up @@ -96,21 +96,9 @@ public function __construct(callable $resolver, callable $onCancelled = null)

$this->close();
};

if (null !== $onCancelled) {
$this->onCancelled = function (Exception $exception) use ($reject, $onCancelled) {
try {
$onCancelled($exception);
} catch (Exception $exception) {
// Caught exception will now be used to reject promise.
}

$reject($exception);
};
} else {
$this->onCancelled = $reject;
}


$this->onCancelled = $onCancelled;

try {
$resolver($resolve, $reject);
} catch (Exception $exception) {
Expand Down Expand Up @@ -217,8 +205,19 @@ public function cancel($reason = null)
$reason = new CancelledException($reason);
}

$onCancelled = $this->onCancelled;
$onCancelled($reason);
if (null !== $this->onCancelled) {
try {
$onCancelled = $this->onCancelled;
$onCancelled($reason);
} catch (Exception $exception) {
$reason = $exception; // Thrown exception will now be used to reject promise.
}
}

$this->result = new RejectedPromise($reason);
$this->result->done($this->onFulfilled, $this->onRejected);

$this->close();
}

/**
Expand Down

0 comments on commit 34f34af

Please sign in to comment.