0

Hello hope everybody is good ? I need some help please ..

I run my JS file and it return in my console with await function = 56

How can i parse the return (56) into my HTML in a <div ?

This is my HTML :

<scrypt> async function getdata() { My scrypt here return 56 in console.log }</scrypt>
<button id="output" onclick="getdata()"style="height: 50px; width: 80px;" > Call it </button>

But when i click on Call it button, nothing is doing

Hope someone can help me ?

2 Answers 2

0

HTML :

  <body>

<script>
    async function getdata() {
const result = await someAsyncFunction();

const elm = document.createElement('div');
const txt = document.createTextNode(result);
elm.appendChild(txt);
  }





 async function someAsyncFunction() {
console.log('56');

 }
</script>
<div>
    <button id="output" onclick="getdata()"style="height: 50px; width: 80px;" > Call it </button>
</div>
</body>
0

You define async function.
I think, you need to use await keyword for some function calling; like this.

async function getdata() {

  // ... your scripts ...

  // someAsyncFunction() returns '56'.
  const result = await someAsyncFunction();

  const elm = document.createElement('div');
  const txt = document.createTextNode(result);
  elm.appendChild(txt);
}

Then, it seems there is a typo in your HTML.
I think <scrypt> shall be <script>.

P.S.
Thank you for adding your scripts.
I was mistaken that why '56' is written in console.
You may add '56' your html body with codes follows;

<body>

<script>
async function getdata() {

  const result = await someAsyncFunction();

  const elm = document.createElement('div');
  const txt = document.createTextNode(result);
  elm.appendChild(txt);

  document.body.appendChild(elm);
}

async function someAsyncFunction() {
  // Do not use console.log() function
  //console.log('56');

  return new Promise(
    (resolveFunc, rejectFunc) => {

      // You should call resolveFunc() when process successfully finished.
      if (true) {
        resolveFunc('56');
      }

      // If you want to treat error cases, you shall use try-catch statement at caller side, like follows;
      //
      // try {
      //   result = await someAsyncFunction();
      // }
      // catch (err) {
      //   alert(err);  // You will watch 'NG!!' message in alert-messagebox, not in console error log.
      // }
      //
      else {
        rejectFunc('NG!!');
      }
    }
  )
}
</script>

<div>
    <button id="output" onclick="getdata()"style="height: 50px; width: 80px;" > Call it </button>
</div>

</body>

Thank you for reading.

2
  • Thanks for answer acutally it return value 56 in my web console i wish it appear in my html body
    – forexmp45
    Commented Nov 9, 2023 at 0:17
  • Thank you for adding your scripts. I added new sample code which is suitable for your script. I wish it helps you. Thank you. Commented Nov 9, 2023 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.