I'm working on a node.js app and using Request to get data from an API, using this function:
function do_request( method, path, body ){
var options = {
...
};
request( options, function(err, response, body){
if (err) console.log(err);
return JSON.parse(response.body);
});
}
I want to be able to save the returned value to a variable - something like this:
var products = do_request('GET','/products','');
At the moment, if I console.log(products)
after the code above, I get undefined
.
From what I've found from googling, it looks like it's probably async-related. I've tried using promises and asyncing functions based on some examples/solutions I found but I'm a node noob and couldn't get it working. Any ideas?