-1

I use redis, and here are my codes

const redis = require("redis"), client = redis.createClient()
let count1, count2, countall
client.hgetall('onekey', function(err, object) {
  if (object) {
    count1 = object.onevalue
    console.log(count1)
  } else {
    count1 = 2
  }
  client.quit()
})
client.hgetall('twokey', function(err, object) {
  if (object) {
    count2 = object.twovalue
    console.log(count2)
  } else {
    count2 = 3
  }
  client.quit()
})
countall = count1 + count2
console.log(countall)

I run this code, and I can get the results of count1 and count2, but console.log(countall) shows undefined, I don't why, how can I pass the value to out of the function block? Many thanks

yupang
  • 155
  • 1
  • 2
  • 16
  • Why you think `countall ` should get the value . It is async call so `console.log(countall)` will excute before the call finished . `Node.js is non blocking i/o , asynchronous programming ` . Check how to use asyn/await , promises . There are lot of questions around. – Himanshu sharma Jul 27 '18 at 06:47

2 Answers2

0

There are various approach you can use , Promises , async/await , async module .

Read them . for your code you can use like this.

I have provided you solution , but to optimized check Promises , async/await , async module.

const redis = require("redis"), client = redis.createClient()
let count1, count2, countall
client.hgetall('onekey', function(err, object) {
  if (object) {
    count1 = object.onevalue
    console.log(count1)
  } else {
    count1 = 2
  }
  client.quit()
  client.hgetall('twokey', function(err, object) {
    if (object) {
      count2 = object.twovalue
      console.log(count2)
    } else {
      count2 = 3
    }
    countall = count1 + count2
    console.log(countall)
    client.quit()
  })
})

Let me explain why that was not working.

Both the client call are asynchronous call , they will execute outside the main thread and other code will execute without waiting for them.

Himanshu sharma
  • 7,487
  • 4
  • 42
  • 75
0
const redis = require("redis"), async = require("async"),client = redis.createClient()
let count1, count2, countall


async.parallel([
    function(callback) {
        client.hgetall('key1', function(err, object) {
      if (object) {
        if(object.count1){
            count1 = Number(object.count1)
        }else{
            count1 = 2 //which is default value you can set if value not found
        }
        callback(null,count1)
      } else {
        callback(err)
      }

    });

    },
    function(callback) {
        client.hgetall('key2', function(err, object) {
      if (object) {
        if(object.count2){
            count2 = Number(object.count2)
        }else{
            count2 = 2 //which is default value you can set if value not found
        }
        callback(null,count2)
      } else {
        callback(err)
      }

    });
    }
],
// optional callback
function(err, results) {
    console.log(results)
        countall = count1 + count2
    console.log("---------------",countall)
});

Node js works on the async manner. So, while redis is busy for fetching the data countall = count1 + count2 statement has been executed already.

Vora Ankit
  • 682
  • 5
  • 8