Nodejs redis hangs if connection fails (neither resolves or rejects)

Viewed 475

If the connection goes away, redis hangs.

const redis = require("redis")
// redis not available on localhost
const client = redis.createClient({
  socket: { connectTimeout: 1000, reconnectStrategy: (r) => r > 5 ? new Error("message") : 500 }
});
let count = 0;
client.on("error", () => console.log(++count))

client.connect().catch(() => console.log("connect failed"));
// neither prints yes nor no
client.get("key").then(() => console.log("yes")).catch(() => console.log("no"))

I want the above client.get() to reject.

Edit 1: (the above gets rejected, still problem with below scenario)

// redis is available on localhost
const redis = require("redis");

const client = redis.createClient({
  socket: { connectTimeout: 1000, reconnectStrategy: (r) => (r > 5 ? new Error("message") : 500) }
});

let count = 0;
client.on("error", () => console.log(++count));

client
  .connect()
  .then(() => console.log("connect success"))
  .catch(() => console.log("connect failed"))
  .then(() => {
    // neither prints yes nor no
    // Place a debugger on next line, once connect success and breakpoint arrives, close redis server and then continue.
    client
      .get("key")
      .then(() => console.log("yes"))
      .catch(() => console.log("no"));
  });
0 Answers
Related