title: How to get redis value for a given redis key using Nodejs + Redis
body: <p>I am using Nodejs + redis to Set and Get key:value pairs. I have written a sample code to set a key:value pair and then fetch it using the default readme from npm redis plugin. </p>
  
  <p>My goal here is to get value from the redis server using any given key. I have followed the steps as given by npm redis plugin. I am able to log it, but since the plugin is async cant figure out a way to get a synchronous <strong>client.get</strong> request. </p>
  
  <p>My code is </p>
  
  <pre><code>var redis = require("redis"),
      client = redis.createClient();
  const bluebird = require("bluebird");
  
  bluebird.promisifyAll(redis.RedisClient.prototype);
  bluebird.promisifyAll(redis.Multi.prototype);
  
  const redisKey = "redis-set-key";
  const redisValue = "hello-world";
  
  client.set(redisKey, redisValue);
  
  function getData(key) {
      return client.get(key, function(err, result) {
          console.log("1. Get key from redis - ", result.toString());
          return result.toString();
      });
  }
  
  const getRedisdata = getData(redisKey);
  console.log("2. getRedisdata", getRedisdata);
  </code></pre>
  
  <p>Result </p>
  
  <pre><code>2. getRedisdata false
  1. Get key from redis -  hello-world
  </code></pre>
  
  <p>My goal is to get the result like this </p>
  
  <pre><code>1. Get key from redis -  hello-world
  2. getRedisdata hello-world
  </code></pre>
  
  <p>Please help me resolve this. </p>
  
  <p>Found a solution, here is my resolved code </p>
  
  <pre><code>const redis = require("redis");
  const client = redis.createClient();
  const bluebird = require("bluebird");
  
  bluebird.promisifyAll(redis.RedisClient.prototype);
  bluebird.promisifyAll(redis.Multi.prototype);
  
  const redisKey = "redis-set-key";
  const redisValue = "hello-world";
  client.set(redisKey, redisValue);
  
  async function setKey(key, value, expire = "EX", time = 300) {
      return new Promise((resolve, reject) =&gt; {
          return client.set(key, value, function(err, result) {
              if (result === null) {
                  reject("set key fail promise");
              } else {
                  resolve(result);
              }
          });
      });
  }
  
  async function getKey(key) {
      return new Promise((resolve, reject) =&gt; {
          return client.getAsync(key).then(function(res) {
              if (res == null) {
                  reject("fail promise");
              } else {
                  resolve(res);
              }
          });
      });
  }
  
  async function hashGetKey(hashKey, hashvalue) {
      return new Promise((resolve, reject) =&gt; {
          return client.hget(hashKey, hashvalue, function(err, res) {
              if (res == null) {
                  reject("hash key fail promise");
              } else {
                  resolve(res.toString());
              }
          });
      });
  }
  
  async function hashGetAllKey(hashKey) {
      return new Promise((resolve, reject) =&gt; {
          return client.hgetall(hashKey, function(err, res) {
              if (res == null) {
                  reject("hash key all fail promise");
              } else {
                  resolve(res);
              }
          });
      });
  }
  
  async function delKey(key) {
      return new Promise((resolve, reject) =&gt; {
          return client.del(key, function(err, result) {
              if (result === null) {
                  reject("delete fail promise");
              } else {
                  resolve(result);
              }
          });
      });
  }
  
  (async () =&gt; {
      // get single key value
      try {
          const keyData = await getKey("string key");
          console.log("Single key data:-", keyData);
      } catch (error) {
          console.log("Single key data error:-", error);
      }
  
      // get single hash key value
      try {
          const hashKeyData = await hashGetKey("hashkey", "hashtest 1");
          console.log("Single hash key data:-", hashKeyData);
      } catch (error) {
          console.log("Single hash key data error:-", error);
      }
  
      // get all hash key values
      try {
          const allHashKeyData = await hashGetAllKey("hashkey");
          console.log("All hash key data:-", allHashKeyData);
      } catch (error) {
          console.log("All hash key data error:-", error);
      }
  
      // delte single key
      try {
          const checkDel = await delKey("XXYYZZ!!!!");
          console.log("Check key delete:-", checkDel);
      } catch (error) {
          console.log("Check key delete error:-", error);
      }
  
      // set single key
      try {
          const checkSet = await setKey("XXYYZZ", "AABBCC");
          console.log("Check data setkey", checkSet);
      } catch (error) {
          console.log("Check data setkey error", error);
      }
  })();
  
  //  hget hashkey "hashtest 1"
  client.hset("hashkey", "hashtest 1", "some value", redis.print);
  client.hset(["hashkey", "hashtest 2", "some other value"], redis.print);
  </code></pre>