Skip to content

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

Closed
@franciscop

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');

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