public function createDeferred()
{
$promise = null;
return new DeferredBridge(['getPromise' => function () use(&$promise) {
if (!$promise) {
throw new Exception(sprintf("[%s] must be cancelled before obtaining the promise.", PromiseCancelled::class));
}
return $promise;
}, 'resolve' => function () {
throw new Exception(sprintf("You cannot call resolve() for [%s].", PromiseCancelled::class));
}, 'reject' => function () {
throw new Exception(sprintf("You cannot call reject() for [%s].", PromiseCancelled::class));
}, 'cancel' => function ($reason = null) use(&$promise) {
if (!$promise) {
$promise = new PromiseCancelled($reason);
}
}]);
}