javascript - Can Bluebird Promise work with redis in node.js? -


here's original code fetch user php session stored in redis:

var session_obj; var key = socket.request.headers.cookie.session  session.get('phpredis_session:'+key,function(err,data){        if( err )        {           return console.error(err);       }       if ( !data === false)       {          session_obj = phpunserialize.unserializesession(data);       }       /* ... other functions ... */ }) 

i rewrite code promise, can't data returned:

promise.resolve(session.get('phpredis_session:'+key)).then(function(data){     return data;  }).then(function(session){       console.log(session); // returns true, not session data     session_obj = phpunserialize.unserializesession(session);  }).catch(function(err){     console.log(err); }) 

the session returned boolean true instead of session data. original redis get function has 2 arguments. assumed promise returned first argument err in original. tried

  promise.resolve(session.get('phpredis_session:'+key)).then(function(err,data){      console.log(data) // return undefined   }) 

but wasn't working either.

does know if redis work promise?

you trying use promise.resolve wrong, expects promise , session.get default doesn't return promise. first need promisify it. (or promisifyall)

session.getasync = promise.promisify(session.get); // or promise.promisifyall(session); //=> `session.getasync` automatically created // or promise.promisifyall(redis); //=> recursively promisify functions on entire redis 

then use new function returns promise use promise, don't need wrap promise.resolve, this:

session.get('phpredis_session:' + key). then(function(data) {     // data }). catch(function(err) {     // handle error err }); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -