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:
- using
async functions
in node 8+
- using
Promises
- using
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/