Reputation: 191
For several days I am struggling in getting Azure Function in Node.js to work with external API. I tried different combinations and libraries like Axios or https, using.then as well as Async Await patterns and in every time during the execution I am getting only Promises in return. I suppose the function gets executed, but I am loosing its context in the main function. Even executing this simple example:
module.exports = async function (context, req) {
context.log('Before the call');
let request_options = {
method: 'GET',
host: 'https://jsonplaceholder.typicode.com',
path: '/todos/1',
headers: {
'Content-Type': 'application/json'
}
};
require('http')
.request(
request_options,
function (res,context) {
context.log(res);
context.log('I am within response');
});
context.log('After the call');
};
Results with:
2019-11-07T13:19:16.169 [Information] Executing 'Functions.Asyncawaittest' (Reason='This function was programmatically called via the host APIs.', Id=769ceae0-257f-432c-af8f-e35504ebc79a)
2019-11-07T13:19:20.880 [Information] Before the call
2019-11-07T13:19:20.881 [Information] After the call
Which indicates that a response is not handled by the context.log context. What am I missing to be able to debug the code within the response? Is it at all executed? I will have some nested API calls to deal with so it will get more complex to debug.
Upvotes: 0
Views: 1366
Reputation: 191
Actually in the mean time I set up my own API endpoint with node-red and ngrok to make it public and found out it fires in that direction. Then I found another related issue here on Stackoverflow on mixing promises with callbacks. So I dropped "async" from the model.exports function declaration and it works now and debugs to logs perfectly. thanks.
Upvotes: 1