I know this is 99% a very stupid question, but I'm just getting started with javascript and node.js.
Here is the code, I'll explain the problem later on:
function f(args){
request.post({stuff},
function optionalCallback(error, response, body) {
if (error) {
console.log('upload failed:', error);
}
console.log(JSON.parse(body)); // #2
return JSON.parse(body);
});
}
// later on in the code, in a switch statement
case 'test':
console.log(f(args)); // #1
break;
Here is the issue I'm having: the console.log() #1 prints out undefined, while the console.log #2 prints out the expected output but after #1 (as in, half a second later)
#1 undefined
#2 [object Object]
I know this is probably a very stupid mistake, but I've been losing my mind on it for hours now
The question is: why does it happen? How can I make sure it prints the object on both cases? (i.e. how do I wait for the function to finish before printing)