From fb255eb5d07eb7f7d93d014852afcf401bc2a9cd Mon Sep 17 00:00:00 2001 From: Francisco Presencia Date: Tue, 19 Sep 2023 06:40:08 +0900 Subject: [PATCH] Have client.connect() return a Promise (#2602) * Connect returns the instance of the client * Added a test * No auto setup * Added a bit of docs * fix the return type, test, and the docs * fix return type * Update packages/client/lib/client/index.spec.ts Co-authored-by: Francisco Presencia --------- Co-authored-by: Leibale Eidelman --- README.md | 8 +++----- packages/client/lib/client/index.spec.ts | 13 +++++++++++++ packages/client/lib/client/index.ts | 5 +++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4fca8d405d4..95816f51e95 100644 --- a/README.md +++ b/README.md @@ -51,11 +51,9 @@ Looking for a high-level library to handle object mapping? See [redis-om-node](h ```typescript import { createClient } from 'redis'; -const client = createClient(); - -client.on('error', err => console.log('Redis Client Error', err)); - -await client.connect(); +const client = await createClient() + .on('error', err => console.log('Redis Client Error', err)) + .connect(); await client.set('key', 'value'); const value = await client.get('key'); diff --git a/packages/client/lib/client/index.spec.ts b/packages/client/lib/client/index.spec.ts index a0824e440ba..4c2899a9b73 100644 --- a/packages/client/lib/client/index.spec.ts +++ b/packages/client/lib/client/index.spec.ts @@ -107,6 +107,19 @@ describe('Client', () => { }); }); + describe('connect', () => { + testUtils.testWithClient('connect should return the client instance', async client => { + try { + assert.equal(await client.connect(), client); + } finally { + if (client.isOpen) await client.disconnect(); + } + }, { + ...GLOBAL.SERVERS.PASSWORD, + disableClientSetup: true + }); + }); + describe('authentication', () => { testUtils.testWithClient('Client should be authenticated', async client => { assert.equal( diff --git a/packages/client/lib/client/index.ts b/packages/client/lib/client/index.ts index c06759f2eca..800894e06bb 100644 --- a/packages/client/lib/client/index.ts +++ b/packages/client/lib/client/index.ts @@ -426,10 +426,11 @@ export default class RedisClient< }); } - connect(): Promise { + async connect() { // see comment in constructor this.#isolationPool ??= this.#initiateIsolationPool(); - return this.#socket.connect(); + await this.#socket.connect(); + return this as unknown as RedisClientType; } async commandsExecutor(