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

162
Vistas
Async function returns promise after .then

In my code, I have an async function that returns a promise.I am aware that this question has been asked before, however none of the solutions worked.

const fetch = require('node-fetch');
async function getData() {
  const response = await fetch(url);
  return (await response.json());
}
getData().then( // wait until data fetched is finished
  console.log(getData())
)

Thank you in advance

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I think you're just getting a little bit confused with the syntax of the .then() callback.

const fetch = require('node-fetch');
async function getData() {
  const response = await fetch(url);
  return (await response.json());
}
getData().then(data => { // Notice the change here
  console.log(data)

  // Now within this block, "data" is a completely normal variable
  // use it as you wish
})

This answer similar in meaning to Guerric's, but hopefully presented in a more beginner-friendly way :)
about 4 years ago · Juan Pablo Isaza Denunciar

0

Remove the useless await and just pass a reference to console.log as the Promise callback:

const fetch = require('node-fetch');

async function getData() {
  const response = await fetch(url);
  return response.json();
}

getData().then(console.log);

If you don't have more control flow in getData, you might not need async/await at all:

const fetch = require('node-fetch');

function getData() {
  return fetch(url).then(x => x.json());
}

getData().then(console.log);
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