skip to Main Content
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


  1. Create a Promise object, and resolve that from inside the callback

    public async validateToken(req, res): Promise<any> {
    
     let resCallback, rejCallback
    
     const returnPromise = new Promise((resolve, reject) => {
        resCallback = resolve
        refCallback = reject
     })
    
     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
          resCallback({  // so this  should be returned as json
            status: 400,
            error: "Invalid Token"
          });
        }
      });
    
     return retPromise
    }
    
    Login or Signup to reply.
  2. Hope this will work

    public async validateToken(req, res): Promise<any> {
    
    const token = req.headers.authorization.split(" ")[1] || req.params.token;
    return await this.redisClient.SISMEMBER("tokenBlackListSet", token, (err,data)=> {
        if (err) { throw new Error(err) }
        else return { status: 400,error: "Invalid Token"};
        }
      });
    }
    
    Login or Signup to reply.
  3. 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 :

    public async validateToken(req, res): Promise<any> {
    
      const token = req.headers.authorization.split(" ")[1] || req.params.token;
    
      return new Promise(function (resolve, reject) { // Here I create a new Promise that will resolve (or reject) when your callback is called
        this.redisClient.SISMEMBER("tokenBlackListSet", token, function(
          err,
          data
        ) {
          if (err) { // If there is an error, we reject the promise
            reject(err);
          }
          else if (data) { // If you have some data, the promise will resolve with the object
            resolve({
              status: 400,
              error: "Invalid Token"
            });
          }
        });
    
      }
    
    }
    

    Notice I removed the await since it is now redundant with the fact that we return a promise already.

    Login or Signup to reply.
  4. 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:

    async function getSISMember() {
      return new Promise((resolve, reject) => {
        this.redisClient.SISMEMBER("tokenBlackListSet", token, function(
          err,
          data
        ) {
          if (err) return reject(err);  // Throw an error if an error is passed to callback
          if (data) { // Return the object you want to return by resolving the promise
            resolve({
              status: 400,
              error: "Invalid Token"
            });
          }
        });
      })
    }
    
    

    await getSISMember() should now either error or give you the JSON response

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search