public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null)
{
if (null !== $this->result) {
return $this->getResult()->then($onFulfilled, $onRejected, $onCancel);
}
if (null !== $this->canceller) {
$this->requiredCancellations++;
$canceller = function ($reason = null) {
if (++$this->currentCancellations >= $this->requiredCancellations) {
return $this->cancel($reason);
}
return null;
};
} else {
$canceller = null;
}
return new static(function ($resolve, $reject, $cancel) use($onFulfilled, $onRejected, $onCancel) {
$this->handlers[] = function (PromiseInterface $promise) use($resolve, $reject, $cancel, $onFulfilled, $onRejected, $onCancel) {
$promise->then($onFulfilled, $onRejected, $onCancel)->done($resolve, $reject, $cancel);
};
}, $canceller);
}