javascript - node.js asynchronous passing variable between functions -


i taking first steps node.js , came across issue passing variable in asynchronous way. have piece of code im using create facebook user:

req.tmppassport = {}; var fb = new fbgraph.facebook(accesstoken, 'v2.2'); function inituser() {     fb.me(function (err, me) {         req.tmppassport.me = me;         console.log(req.tmppassport.me) // works     }); }  console.log(req.tmppassport.me) // not working -> undefined var 

i tried figure out why second log isn't working , ended reading synchronous , asynchronous functions, in attempt implement read tried coming solution using callbacks, no success. last attempt this:

req.tmppassport = {}; var fb = new fbgraph.facebook(accesstoken, 'v2.2'); function inituser() {     fb.me(function (err, me) {         req.tmppassport.me = me;     });     fb.my.events(function (err, events) {         //console.log(events);         req.tmppassport.events = events;      });     fb.my.friends(function (err, result) {         req.tmppassport.results = result;     }); } function passuser(){     console.log(req.tmppassport);     return req.tmppassport; } cp.exec(inituser, passuser); 

but not working... trying achieve render object express router var looks this:

   router.get('/welcome', securepages, function(req, res, next){         res.render('welcome', {title:'welcome adating', user:req.tmppassport});     }) 

but cant figure out how pass object after created...any please?

a method of chaining function calls when async tasks done 1 way deal this.

looking @ first snippet of code, rewritten follows:

req.tmppassport = {}; var fb = new fbgraph.facebook(accesstoken, 'v2.2'); function inituser() {     fb.me(function (err, me) {         console.log(req.tmppassport.me) // works         req.tmppassport.me = me;          // triggers execution of next step         post_populating_passport();     }); }  function post_populating_passport() {     // function executed after callback async call     console.log(req.tmppassport.me); } 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -