public async validateToken(req, res): Promise<any> {
const token = req.headers.authorization.split(" ")[1] || req.params.token;
await this.redisClient.SISMEMBER("tokenBlackListSet", token, function(
err,
data
) {
if (data) { // here i get data=1 as token is present in tokenBlackListSet
return { // so this should be returned as json
status: 400,
error: "Invalid Token"
};
}
});
}
// in other async function
const response = await validateToken(req,res);
console.log(response) // returned value is always undefined
4
Answers
Create a
Promise
object, and resolve that from inside the callbackHope this will work
Since the
SISMEMBER
method return a boolean, and not the value returned by the callback, you can return a new Promise that you resolve in the callback :Notice I removed the
await
since it is now redundant with the fact that we return a promise already.I do not think that the
this.redisClient.SISMEMBER
will wrap your callback result as a promise. If it would be case, you wouldn’t be one here to ask the question.Your result is lost in the void at the moment as the callback result is not passed as a return value.
Wrap the function into a promise and resolve or reject it when the callback is invoked by your library:
await getSISMember()
should now either error or give you the JSON response