pradeep desai
pradeep desai

Reputation: 13

async function does not return json object

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

Upvotes: 1

Views: 3725

Answers (4)

Jeffrey Devloo
Jeffrey Devloo

Reputation: 1426

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

Upvotes: 0

Seblor
Seblor

Reputation: 7136

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.

Upvotes: 4

Radu Diță
Radu Diță

Reputation: 14181

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
}

Upvotes: 0

Kiwi Rupela
Kiwi Rupela

Reputation: 2348

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

Upvotes: 1

Related Questions