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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -