Skip to content
Advertisement

Nodejs wait till async function completes and print the results

I want to wait on the HTTP POST request to complete and then return response to the caller function. I am getting Undefined when I print the received results.

I have defined post method as below:

JavaScript

And I am calling it in this way:

JavaScript

I don’t want to handle the .then and .catch in the calling function as I want to return the different values based on the POST result.

How should I wait for the response and get the return results.
In the above code I am getting the log statements as expected and “Done Result” get printed at the very end after the ‘getPostResult’ returns.

Advertisement

Answer

you are using both await & .then thats why it returns undefined.

this is how it should look

JavaScript

if you want to catch error outside of this function then this is way to go.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement