-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?

8
  • Can you do return await db.one()? If yes, problem solved. If not, you'll need to use a Promise Commented May 25, 2020 at 7: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. Commented May 25, 2020 at 7: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. :) Commented May 25, 2020 at 7: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) { Commented May 25, 2020 at 7: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? Commented May 25, 2020 at 7:59

1 Answer 1

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 );
}
Sign up to request clarification or add additional context in comments.

6 Comments

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?
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.
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
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?
ah, this is getting complicated. Can't you just put the graphql resolver inside doSomethingWith() ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.