Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

432
Views
Async JS : The console log displays the data as text but the HTML DIV renders [Promise Object]

I have a dynamic resultArray numbers.The console.log(data) shows the data text I want from the API. But the div in the HTML returns a [Object Promise] .

let resultArray = ['6']

  const getUrl = (num) => `http://numbersapi.com/${num}`;
  const getFacts = async (num) => {
    try {
      const resp = await fetch(getUrl(num));
      const data = await resp.text();
      console.log(data);
      return data;
    } catch (error) {
      console.log(error);
    }
  };

let theQuote = document.getElementById("quote");
theQuote.textContent = getFacts(resultArray);  // [object promise]
<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
  <body class="main">
    <div id="quote"></div>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I don't sure but it may work for you.

(async () => {
    let theQuote = document.getElementById("quote");
theQuote.textContent = await getFacts(resultArray);
})();
about 4 years ago · Juan Pablo Isaza Report

0

cuz your function getFacts() return a promise and you need to resolve it when use it , try to render data inside the function to avoid this

let theQuote = document.getElementById("quote");
let resultArray = ['6']

  const getUrl = (num) => `http://numbersapi.com/${num}`;
  const getFacts = async (num) => {
    try {
      const resp = await fetch(getUrl(num));
      const data = await resp.text();
      console.log(data);
theQuote.textContent = data;  
    } catch (error) {
      console.log(error);
    }
  };
 
about 4 years ago · Juan Pablo Isaza Report

0

Basically you just need to wait until the promise from getFacts gets resolved. You can achieve that by using then method of the promise like below:

getFacts(resultArray).then((facts) => {
  let theQuote = document.getElementById("quote");
  theQuote.textContent = facts;
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!