onDone = $this->callableFromInstanceMethod("done"); $this->watcher = Loop::repeat(\PHP_INT_MAX / 2, function () { // do nothing, it's a dummy watcher }); Loop::disable($this->watcher); Loop::setState(self::class, new class($this->watcher) { private $watcher; private $driver; public function __construct(string $watcher) { $this->watcher = $watcher; $this->driver = Loop::get(); } public function __destruct() { $this->driver->cancel($this->watcher); } }); } public function listen(Promise $promise) { if ($this->requests++ === 0) { Loop::enable($this->watcher); } $promise->onResolve($this->onDone); } private function done() { if (--$this->requests === 0) { Loop::disable($this->watcher); } \assert($this->requests >= 0); } }