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

309
Views
registrando la respuesta.json() dentro del método entonces() antes de devolver la promesa de respuesta.json()

Estoy aprendiendo desarrollo web, así que tal vez la pregunta ya esté hecha pero no la he encontrado. Intenté registrar response.json() dentro del método .then antes de devolver la promesa de response.json() a la segunda función del controlador .then . El problema al que me enfrento es que, aunque se registra el resultado de response.json() , el código del siguiente método .then no se ejecuta. Aquí está mi código:

 const fetchPromise = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); console.log(fetchPromise) fetchPromise .then( response => { console.log(response.json()) return response.json(); }) .then( json => { console.log(json[0].name); });

Se supone que el código registra el resultado de response.json() y luego termina con el registro de "baked beans" en el segundo controlador .then , pero solo registra el objeto de promesa y no sucede nada después. Este código, sin embargo, parece funcionar:

 const fetchPromise = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); console.log(fetchPromise) fetchPromise .then( response => { var resp=response.json() // save the value first console.log(resp) return resp }) .then( json => { console.log(json[0].name); });

Entonces, ¿por qué almacenar el resultado en una variable funciona, pero llamarlo directamente no?

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

0

En la consola, verá este error:

 Uncaught (in promise) TypeError: Failed to execute 'json' on 'Response': body stream already read

Eso es porque no puedes ejecutar response.json() dos veces . Simplemente mueva console.log hasta el último .then y registre json en lugar de res.json :

 fetchPromise .then( response => { console.log(response.json()) // remove this line return response.json() }) .then( json => { console.log(json); // add this line console.log(json[0].name); });
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!