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

160
Views
La función asíncrona devuelve la promesa después de .then

En mi código, tengo una función asíncrona que devuelve una promesa. Sé que esta pregunta se ha hecho antes, sin embargo, ninguna de las soluciones funcionó.

 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()) )

Gracias de antemano

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

0

Creo que te estás confundiendo un poco con la sintaxis de la devolución de llamada .then() .

 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 })

Esta respuesta tiene un significado similar al de Guerric, pero con suerte se presenta de una manera más amigable para los principiantes :)
about 4 years ago · Juan Pablo Isaza Report

0

Elimine la await inútil y simplemente pase una referencia a console.log como la devolución de llamada de Promise :

 const fetch = require('node-fetch'); async function getData() { const response = await fetch(url); return response.json(); } getData().then(console.log);

Si no tiene más flujo de control en getData , es posible que no necesite async / await en absoluto:

 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 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!