如何从箭头函数内部返回变量?

时间:2021-06-18 20:21:19

标签: javascript node.js redis arrow-functions

我是 node 的新手,我很难获得一个非常基本的东西。

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

我想访问第 5 行中第 3 行的变量 reply。直到第 3 行才出现问题。 一个简单的函数而不是箭头函数也适用于我。

1 个答案:

答案 0 :(得分:0)

你需要使用承诺。


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

          if (err) {
                return rej(err)
           }

           return res(data);
     });
  });
}