1
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
7
  • 3
    validateToken has no return statement Commented Dec 18, 2019 at 10:10
  • what if i want to return from redisClient.SISMEMBER's callback function Commented Dec 18, 2019 at 10:11
  • Then you need to return a Promise that you resolve in your callback (with your object as argument in the resolve method call) Commented Dec 18, 2019 at 10:11
  • i changed as you suggested , but still it is not working return await this.redisClient.SISMEMBER( "tokenBlackListSet", token, function(err, data) { if (data) { return new Promise(resolve => resolve({ status: 400, error: "Invalid Token" }) ); } } ); Commented Dec 18, 2019 at 10:23
  • I have written an answer, I hope it can help you. Commented Dec 18, 2019 at 10:24

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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"};
    }
  });
}

3 Comments

Same as Radu's answer : Are you sure SISMEMBER will resolve with the returned value from the callback ?
looks like await here is redundant.
this.redisClient.SISMEMBER returns a promise that can be resolved by a caller of validateToken function.
0

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
}

2 Comments

Are you sure SISMEMBER will resolve with the returned value from the callback ?
i need to return json based on 'data' variable's value in callback function
0

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

1 Comment

Checkout @Seblor's answer as he implemented the function as a whole. I just ensured the result of your function would be resolved into a promise, which he did too.

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.