Skip to content

Commit fb255eb

Browse files
franciscopleibale
andauthored
Have client.connect() return a Promise<RedisClient> (#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 <[email protected]> --------- Co-authored-by: Leibale Eidelman <[email protected]>
1 parent 5a10826 commit fb255eb

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ Looking for a high-level library to handle object mapping? See [redis-om-node](h
5151
```typescript
5252
import { createClient } from 'redis';
5353

54-
const client = createClient();
55-
56-
client.on('error', err => console.log('Redis Client Error', err));
57-
58-
await client.connect();
54+
const client = await createClient()
55+
.on('error', err => console.log('Redis Client Error', err))
56+
.connect();
5957

6058
await client.set('key', 'value');
6159
const value = await client.get('key');

packages/client/lib/client/index.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ describe('Client', () => {
107107
});
108108
});
109109

110+
describe('connect', () => {
111+
testUtils.testWithClient('connect should return the client instance', async client => {
112+
try {
113+
assert.equal(await client.connect(), client);
114+
} finally {
115+
if (client.isOpen) await client.disconnect();
116+
}
117+
}, {
118+
...GLOBAL.SERVERS.PASSWORD,
119+
disableClientSetup: true
120+
});
121+
});
122+
110123
describe('authentication', () => {
111124
testUtils.testWithClient('Client should be authenticated', async client => {
112125
assert.equal(

packages/client/lib/client/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,11 @@ export default class RedisClient<
426426
});
427427
}
428428

429-
connect(): Promise<void> {
429+
async connect() {
430430
// see comment in constructor
431431
this.#isolationPool ??= this.#initiateIsolationPool();
432-
return this.#socket.connect();
432+
await this.#socket.connect();
433+
return this as unknown as RedisClientType<M, F, S>;
433434
}
434435

435436
async commandsExecutor<C extends RedisCommand>(

0 commit comments

Comments
 (0)