0

I am learning nodejs and stuck . Need your help. When I run function getSessionKey() function... I am able to log session key inside request call back but not able to log it outside request. Please help me how can access it outside.

function getSessionKey(){
var key = '';
var tempObj = {method:'',id:0,params:{username:'',password:''}};
tempObj.id = 1;
tempObj.params.username = 'test';
tempObj.params.password = 'test';
tempObj.method = 'get_session_key';
options.body = JSON.stringify(tempObj);
        request(options, function (error, response, body) {
            if (error) throw new Error(error);
            var keyObj = JSON.parse(body);
            key = keyObj.result;
            console.log(key); //here I am able to access key
          });
//console.log(key); //but I want to get it here.
}
getSessionKey()
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72
Triven
  • 351
  • 1
  • 4
  • 17
  • You don't. You learn how to program asynchronously where you use asynchronous results only in the async callback itself or in a function you call from there. This is how async programming works in nodejs. – jfriend00 Apr 25 '16 at 04:33
  • Thank you.. I will review other thread where this similar question is answered. – Triven Apr 25 '16 at 04:36

0 Answers0