I want to get the klout score using a screen name (twitter).
I did this. I know it doesn't work like that, but I don't know how it should work.
function get_klout(screenName){
klout.getKloutIdentity(screenName, function(error, klout_user) {
klout.getUserScore(klout_user.id, function(error, klout_response) {
return Math.round(klout_response.score);
});
});
}
I want my function to return this number Math.round(klout_response.score);
function get_klout(screenName) {
klout.getKloutIdentity(screenName, function(error, klout_user) {
klout.getUserScore(klout_user.id, function(error, klout_response) {
return Math.round(klout_response.score);
});
});
}
Your function is async, so you can't just assign what it returns to a variable because you will just assign undefined:
var result = get_klout('foo'); // undefined
what you can do is:
async functions
in node 8+
Promises
callbacks
:function get_klout(screenName, done) {
klout.getKloutIdentity(screenName, function(error, klout_user) {
klout.getUserScore(klout_user.id, function(error, klout_response) {
done(Math.round(klout_response.score));
});
});
}
get_klout('foo', function(response) {
console.log(response);
});
just a note:
In node is a common pattern implementing the error first callback
and you should have a look on it because it's the traditional and more used way to handle errors:
http://fredkschott.com/post/2014/03/understanding-error-first-callbacks-in-node-js/