Skip to content

Have client.connect() return a Promise<client> instead of Promise<void> #2601

Closed

Description

@franciscop

Motivation

It's not possible to create a client in a single step that defers the creation (even if async), so now if we need a promise of the client we need to do this:

const storeProm = (async() => {
  const client = createClient();
  await client.connect();
  return client;
})();
// Can use the client promise

If I want to use Redis as a KV store, it needs to be initialized and awaited:

const client = createClient();

// Need to await, which we might not yet be in the right place to do so
await client.connect();

const api = fch.create({ cache: { store: client } });

However it'd be nice if we could join both steps into one, so that it could be done in a single step without the extra syntax:

const store = await createClient().connect();
const api = fch.create({ cache: { store }});

More specifically, I have a library called Swear that is able to very nicely wrap the creation promise and avoid any await at all until the "leaf" function is called and awaited:

// Currently
const store = swear((async() => {
  const client = createClient();
  await client.connect();
  return client;
})());
await store.set('a', 'b');
await store.get('a');

// If .connect() returned a Promise<client>
const store = swear(createClient().connect());
await store.set('a', 'b');
await store.get('a');

Basic Code Example

// Would love to do this:
const store = await createClient().connect();
const api = fch.create({ cache: { store }});

// Which would also unlock also this with my library `swear`:
const store = swear(createClient().connect());
await store.set('a', 'b');
await store.get('a');

Activity

  1. franciscop commented on Aug 17, 2023

    @franciscop
    ContributorAuthor

    I'd be happy to try giving it a stab at it, I believe it would be here:

    // Before
    connect(): Promise<void> {
        // see comment in constructor
        this.#isolationPool ??= this.#initiateIsolationPool();
        return this.#socket.connect();
    }
    
    // After
    async connect(): Promise<RedisClientType> {
        // see comment in constructor
        this.#isolationPool ??= this.#initiateIsolationPool();
        await this.#socket.connect();
        return this;
    }
  2. leibale commented on Aug 17, 2023

    @leibale
    Contributor

    I like this idea! :) Wanna open a PR?

    BTW, don't forget to add a listener to error events as well, otherwise it will crash your application (see here)

  3. bigDP commented on Nov 28, 2023

    @bigDP

    Hi, Can i take this task?

  4. leibale commented on Nov 28, 2023

    @leibale
    Contributor

    @bigDP This is already done in #2602 (and should've been closed when I merged it, which did not happen for some reason). Sorry for the confusion, and thanks for wanting to contribute! :)

  5. franciscop commented on Jan 20, 2026

    @franciscop
    ContributorAuthor

    Just wanted to come back to thank again for accepting this, I integrated it into my library Polystore and it works great!

    import kv from "polystore";
    import { createClient } from "redis";
    
    const store = kv(createClient().connect());
    
    await store.set("key1", "Hello world", { expires: "1h" });
    console.log(await store.get("key1"));
    // "Hello world"

    https://polystore.dev/documentation#redis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions