node.js - Node Redis Async -
i have simple function tokenexists
checks see whether token part of redis set
. know resp
coming undefined
because final line of code running before function done making request , getting response redis
. have read many articles on handling async
nature of redis
node
of had either multi-line commands or express
, route specific issues. wondering how make code synchronous.
function tokenexists(token, callback) { db.sismember("tokens", token, function(err,res) { if (err) throw err; callback(null, parseint(res, 10) === 1); }); } function generatetoken(){ try { var token = urlsafebase64(crypto.randombytes(23)); } catch (ex) { console.log(ex) } tokenexists(token, function(err,res){ if (err) throw err; res ? token : generatetoken(); }) } ^^ method returning 'undefined'
you can't.
the database call async have use callback tokenexists function.
function tokenexists(token, callback) { db.sismember("tokens", token, function(err,res) { if (err) throw err; callback(null, parseint(res, 10) === 1); }); }
Comments
Post a Comment