connectionManager = $connectionManager; $this->timeout = $timeout; $this->loop = $loop ?: Loop::get(); } public function connect($uri) { $promise = $this->connectionManager->connect($uri); return Timer\timeout($promise, $this->timeout, $this->loop)->then(null, function ($e) use ($promise) { // connection successfully established but timeout already expired => close successful connection $promise->then(function ($connection) { $connection->end(); }); throw $e; }); } }