0

I have a function in NodeJs

function ga_data(view, callback) { 

    overview = {orgaincCount:12,sessionsCount:3,newUserCount:4,sessionArray:session};

    callback(1, overview);
}

Calling a fucntion

ga_data(view, function(err, result)  {
if (err == 0) {
  jsonString = {err : 1, result : []}
} else {
    // res.send({status: 1, message: "Success",overview: result});
    jsonString = {err : 0, result : result}
}

});

Now I want to access the callback result outside the callback function i.e.

ga_data(view, function(err, result) {
if (err == 0) {
   jsonString = {err : 1, result : result};
} else {
    jsonString = {err : 0, result : result};
}
});
console.log(jsonString);
tafsir
  • 49
  • 1
  • 3
  • 9
  • you can't - because asynchronous code is asynchronous and nothing will change that – Jaromanda X Apr 16 '18 at 06:09
  • You need another callback. – deEr. Apr 16 '18 at 06:10
  • To access the callback result outside the callback function you can emit update event, and handle that 'update' event. Create jsonString as an event emitter and subscribing to events. var EventEmitter = require("events").EventEmitter; var jsonString = new EventEmitter(); jsonString.emit('update'); jsonString.on('update', function () { console.log(jsonString.result); // THIS WORKS! }); – Swati Apr 16 '18 at 06:25

0 Answers0