javascript - Use result of asynchronous chrome.cookies.getAll -


i running asynchronous javascript problem, having trouble saving value received in callback method object literal. making chrome extension , using chrome.cookies.getall method , able cookies , read them in callback, not able save value object. still getting hang of objects , asynchronous javascript , use help.

here's code

var app = {     init: function() {         this.activecookie = {             'sid': null         };         this.setupsession();         // shows null         console.log('session in object : ');         console.log(this.activecookie['sid']);     },      setupsession: function() {         var = this;          function setcookiesasync(that, cookie) {             console.log(cookie);             for(var i=0; i<cookie.length; ++i) {                 if(cookie[i]['name'] === 'sid') {                     that.activecookie['sid'] = cookie[i]['value'];                     // shows token                     console.log('session in callback : ');                     console.log(that.activecookie['sid']);                 }             }         }          chrome.cookies.getall({             'url': st.baseurl         }, function (cookie) {             setcookiesasync(that, cookie);          });     } }; 

since callback executes after

console.log('session in object : '); console.log(this.activecookie['sid']); 

this.activecookie['sid'] null.

i wondering how can save value object asynchronous method saves object , subsequent lines of code execute after setupsession() complete.

while question on very canonical topic, it's worded , scoped enough providing specific solution helpful.

parts of init() executed asynchronously. means time init() finishes executing, not of code yet run. there no way around - code after async part run before it.

however, if have code want execute after init (like, instance, rest of code!), can passing code callback , adding appropriate place:

init: function(callback) {   this.activecookie = {     'sid': null   };   this.setupsession(callback); },  setupsession: function(callback) {   var = this;    function setcookiesasync(that, cookie) {     for(var i=0; i<cookie.length; ++i) {       if(cookie[i]['name'] === 'sid') {         that.activecookie['sid'] = cookie[i]['value'];       }     }     // point of init() has finished     if (typeof callback === "function") { callback(); }   }    chrome.cookies.getall({     'url': st.baseurl   }, function(cookie) {     setcookiesasync(that, cookie);   }); } 

then can use code follows:

app.init(function() {   /* code should execute after init */ }); 

alternatively, can use promises.

init: function() {   this.activecookie = {     'sid': null   };   return this.setupsession(callback); // returns promise },  setupsession: function() {   return new promise(function(resolve, reject) {     var = this;      function setcookiesasync(that, cookie) {       for(var i=0; i<cookie.length; ++i) {         if(cookie[i]['name'] === 'sid') {           that.activecookie['sid'] = cookie[i]['value'];         }       }       // point of init() has finished       resolve();     }      chrome.cookies.getall({       'url': st.baseurl     }, function (cookie) {       setcookiesasync(that, cookie);     });   }); } 

and usage becomes:

app.init().then(function() {   /* code should execute after init */ }); 

Comments