-1

I need to use redis async functions. Currently I am using the redis library. My require ment is that in this library im using the exists function to check if a key is in redis or not. If not i'm making a DB call inside this exists function and trying to return the DB response. Here is the part of code: -

var redis = require('redis');
var client = redis.createClient(port, 'anyhost');
client.exists(obj.empId, function(err, reply) {

  if (reply == 0) {
    console.log('indb call');
    return db.one('SELECT * FROM iuidtest WHERE empid = $1', [obj.empId])
      .then(iuidtest => {
        console.log(iuidtest.iuid);
        return iuidtest.empid;

      })
  }
});

Here I am able to print the iuid value in console but not return the value from it. I read somewhere the reason maybe that I am returning value from async method db.one inside a sync method client.exists. So I tried using the redis-async library.

var asyncredis = require('async-redis');
var myCache= asyncredis.createClient(port, 'vsseacgmy13');

But here this myCache variable does not have the redis functions like exists() that were in client variable. My requirement is here to return the DB call value after checking the key in cache. Is there any way like using another lib or making this exists function async so I can return the value of DB call?

palaѕн
  • 72,112
  • 17
  • 116
  • 136
noob
  • 74
  • 1
  • 8
  • Can you do `return await db.one()`? If yes, problem solved. If not, you'll need to use a Promise – Jeremy Thille May 25 '20 at 07:07
  • No when using await with db.one i'm not able to do so as the error message is that await can only be used inside an async function. So i figured exists function must be synchronous. – noob May 25 '20 at 07:29
  • Also I am using promise inside db,one to get the result and log it to console. I am even able to return from this function if it is inside some async function. Can you please explain how to use promises to resolve my problem as I am unable to understand. Any help is greatly appreciated. :) – noob May 25 '20 at 07:41
  • `await can only be used inside an async function` Yes of course, so just add `async` before your function name. `client.exists(obj.empId, async function(err, reply) {` – Jeremy Thille May 25 '20 at 07:51
  • Yes this way I was able to add await to the db.one function. But unfortunately i'm still not able to return the value from this db.one function. I think the problem might be that my redis exists function is synchrnous so I can't return value from an async function(db.one) inside it. Or am I wrong? – noob May 25 '20 at 07:59
  • The Redis method is asynchrounous, because it takes a callback function. So is `db.one`. So you need to wait for both to complete before returning the final result. Have a look at https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Jeremy Thille May 25 '20 at 10:13
  • If that is the case then why am i able to log the value here but not return it. And even if I try to return some random string inside this exists function this doesn't work. I do not understand from the above link how to solve it. Can you please provide the code for my scenario? – noob May 25 '20 at 10:24
  • Instead of `return iuidtest.empid`, which returns the value into nothing, simply pass it to another function. `doSomethingWith(iuidtest.empid);` – Jeremy Thille May 25 '20 at 10:27

1 Answers1

0

After the two async functions are completed, simply pass the final value to a new function and do some work with it.

var redis = require('redis');
var client = redis.createClient(port, 'anyhost');

client.exists(obj.empId, function(err, reply) {
  if (reply == 0) {
    console.log('indb call');
    db.one('SELECT * FROM iuidtest WHERE empid = $1', [obj.empId])
      .then(iuidtest => {
        console.log(iuidtest.iuid);
        doSomethingWith(iuidtest.empid);
      })
  }
});

const doSomethingWith = empid => {
  console.log( "empid = ", empid );
}
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • Thanks. Just another thing still i'm having trouble returning the empid value from the doSomethingWith function. Since now I have got this value inside a synchronous function how do I return it? – noob May 25 '20 at 10:48
  • What this does is pass the value in the function and in the function value is logged. Isn't it the same thing as logging the value in the async function only? As if I add the return statement in the doSomethingWith func declaration it does not return the value as it is this return statement is called in the async func anyways. Nor can I set any this value to any global var in the doSomethingWith func and then return that as these async functions are executed afterwards. – noob May 25 '20 at 11:10
  • Why do you want to "return" this value? Return to where? Just use it, it's defined in a function, just do your stuff with it – Jeremy Thille May 25 '20 at 12:48
  • Beacuse this db call I am doing inside a graphql resolver function that function expects to return a string value. So basically this value is to be returend from that resolver function.Shall i share more of the code for clarification? – noob May 25 '20 at 13:07
  • ah, this is getting complicated. Can't you just put the graphql resolver inside `doSomethingWith()` ? – Jeremy Thille May 25 '20 at 13:27
  • That's not how the resolver works actually. But I was able to return the value by promisfying the exists function and wrapping it inside an async function. This way i was able to use await with my defined asnc method and store it's response in a const. Using this I was able to return the value. – noob May 25 '20 at 13:40