0

I am newbie to node and I am having hard time in getting a very basic thing.

    redis_client.get('foo', (err, reply) => {
      if (err) throw err;
      console.log(reply);
    });
    console.log(reply);

I want to access variable reply of line 3 in line 5. There is no issue till line 3. A simple function instead of an arrow function will also work for me.

quickhaze
  • 286
  • 1
  • 12

1 Answers1

0

You need to use a promise.


export function getFromCache (key) {
  return new Promise((res, rej)=>{ 
     return redis.get(key, (err,data)=>{

          if (err) {
                return rej(err)
           }

           return res(data);
     });
  });
}
Ernesto
  • 3,944
  • 1
  • 14
  • 29