Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

javascript - Redis async library does not have function that are in redis library for node.js

I need to use redis async functions. Currently I am using the redis library. My require ment is that in this library im using the exists function to check if a key is in redis or not. If not i'm making a DB call inside this exists function and trying to return the DB response. Here is the part of code: -

var redis = require('redis');
var client = redis.createClient(port, 'anyhost');
client.exists(obj.empId, function(err, reply) {

  if (reply == 0) {
    console.log('indb call');
    return db.one('SELECT * FROM iuidtest WHERE empid = $1', [obj.empId])
      .then(iuidtest => {
        console.log(iuidtest.iuid);
        return iuidtest.empid;

      })
  }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After the two async functions are completed, simply pass the final value to a new function and do some work with it.

var redis = require('redis');
var client = redis.createClient(port, 'anyhost');

client.exists(obj.empId, function(err, reply) {
  if (reply == 0) {
    console.log('indb call');
    db.one('SELECT * FROM iuidtest WHERE empid = $1', [obj.empId])
      .then(iuidtest => {
        console.log(iuidtest.iuid);
        doSomethingWith(iuidtest.empid);
      })
  }
});

const doSomethingWith = empid => {
  console.log( "empid = ", empid );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...