Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

426
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda