isEmpty()) { /** @var self $dfd */ $dfd = $q->dequeue(); $dfd->run(); } } public function __construct(callable $callback) { $this->callback = $callback; $this->promise = new SyncPromise(); self::getQueue()->enqueue($this); } public function then($onFulfilled = null, $onRejected = null) { return $this->promise->then($onFulfilled, $onRejected); } public function run() : void { try { $cb = $this->callback; $this->promise->resolve($cb()); } catch (Exception $e) { $this->promise->reject($e); } catch (Throwable $e) { $this->promise->reject($e); } } }