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

JavaScript async function returns object Promise

I’m trying to figure out, how to return from JavaScript async function.

In Node.js application script.js I have a function:

  let result = getresult(input);

must be async function with /content tag in await fetch to insert, send msg: input value to server.js functions, and return result value. In script.js:

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

async function getresult(input) {
  const request = await fetch(
    `/content?${new URLSearchParams({
      msg: input,
    })}`
  );
  const serverRes = await request.json();
  console.log("Server response: ", serverRes );
  return serverRes ;
}

Chrome console successfully shows requested string Server response: {text: 'hello!'}, but return returns [object Promise]

I’m not sure, what I’m doing incorrectly.

>Solution :

let result = await getresult(input);

You must await the result of your asynchronous function.

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