I know it is the most frquently asked question in javascript regarding asynchronous behaviour of functions. Still I am not able to figure out a working solution for my use case. What I am trying to do is lookup the redis cache using a key. I am using the exists function to check the key is present or not. If present then i'll return from cache if not then i'll set the key and then make a db call using this key as a db param. It seems very simple but no matter what I do i'm unable to return the value from the cache or the DB. If I make these calls outside the exist function then it works as the resolver function(graphql resolver) is an async function. This resolver functions expects a return value. So here is the code by which i'm unable to retun the value in any scenario:-
empId: async(obj, params, ctx, resolverInfo) => {
await client.exists(obj.empId, async function(err, reply) {
if (reply == 1) {
return await getAsync(obj.empId).then(res => {
console.log(res);
return res;
})
} else {
return await db.one('SELECT * FROM iuidtest WHERE empid = $1', [obj.empId])
.then(iuidtest => {
console.log(iuidtest.empid);
return iuidtest.empid;
})
}
});
const doSomethingWith = empid => {
console.log("empid = ", empid);
}
I am getting the correct values in console but am unable to return. However if I directly make these calls in my resolver function i.e. outside of the redis exists function I am able to return the value.
empId: async(obj, params, ctx, resolverInfo) => {
return await getAsync(obj.empId).then(res => {
console.log(res);
return res;
This way I am able to return the value from the resolver function. It would be really of great help if anybody can provide the working code for this instead of other links regarding how to return from async function using callbacks and promises. Here is another post reg the same. :- Redis async library does not have function that are in redis library for node.js Thanks in advance!