Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return a value in NodeJS inside of an Async function?

I have the following code:

async function myPromiseFunction() {
  return "test";
};

function processComment(params) {
    (async () => {
          console.log(await myPromiseFunction());
    })();
}

exports.main = processComment;

The only way to output HTML code to the DOM in this case (serverless function), is through return {"body": "<h1>Test</h1>"}.

The problem is, if I put return {"body": "<h1>Test</h1>"} inside of the Async, it does not work and just does not return anything – it only works inside the processComment function whilst it is outside an async.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I replace console.log(await myPromiseFunction()); with return {"body": await myPromiseFunction())?

I can only console.log the value, but how can I return it so that it gets outputted as HTML?

>Solution :

I think updating your processComment() method like this would do the trick. Please let me know if that doesn’t work so I can update this answer 😀

function processComment(params) {
    return (async () => {
          var data = await myPromiseFunction();
          return { "body": data };      
    })();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading