nn3112337 nn3112337 - 4 months ago 11
Node.js Question

Returning a function with a callback inside

Heres my code. I basicly want to return the 'body' var in my getFollows function. Setting vars obviously dont work, no idea how to get the variable. I can't change getUserFollowedChannels because its a package, and I need to return it to the function directly because of meteor server->client stuff.

'twitch.getFollows'() {
var followers = twitch.getUserFollowedChannels('atlatonin', function(err, body) {
if (err) {
return err;
} else {
console.log(body.follows[0].channel.display_name);
return body.follows[0].channel.display_name;
}
});
return followers;
},

Answer

You can pass a callback function as below:

'twitch.getFollows': function(done) {
     twitch.getUserFollowedChannels('atlatonin', done);
 }

And invoke the function as below:

twitch.getFollows(function(err, body) {
    if (err) {
        console.log(err);
        //return err;
    } else {
        console.log(body.follows[0].channel.display_name);
        //return body.follows[0].channel.display_name;
    }
});
Comments