I'm working on a Node.JS function that has to do 2 things - request some data from an API, process it and return it to the caller.
The trouble is that the API call (using https
module) is asynchronous and my function returns before the API call returns the data. Here is my code:
const https = require('https');
const options = {
hostname: 'tmp-s.s3.amazonaws.com',
port: 443,
path: '/groups.json',
method: 'GET',
};
const MyHandler = {
handle(handlerInput) {
var group_name;
api_call = function() {
return new Promise( (resolve, reject) => {
req = https.request(options, function(res){
var data = '';
//console.log('statusCode:', res.statusCode);
//console.log('headers:', res.headers);
res.on('data', function(d){ data += d; });
res.on('end', function(){
console.log(data);
var _groups = JSON.parse(data);
var _group_name = _groups[1].name;
console.log("In res.on(): " + _group_name);
resolve(_group_name); // RESOLVE the promise
});
});
req.end();
});
}
api_call().then((value) => {
group_name = value; // I have to return this _value_ from MyHandler.handle()
return group_name; // This returns from the then()-function, not from MyHandler.handle()
});
return group_name; // This returns before group_name is set :(
}
}
// Just for testing - something else calls MyHandler.handle()
console.log("In main(): " + MyHandler.handle("blah"));
The above prints:
In main(): undefined
In res.on(): Group 1
What I want instead is the reversed sequence and no undefined results:
In res.on(): Group 1
In main(): Group 1
Someone closed my previous question as a duplicate for This question but I still can't make it work. Tried to add the Promise
but still unable to wait for the completion in my handle()
function. I have no control over the caller of MyHandler.handle()
- it calls my function and I have to return the API call result. I can't change anything in the caller.
I would much prefer if someone could help me with my sample code rather than closing it again as a duplicate of something remotely similar that I can't make work. Please :)