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

172
Vistas
Access response object from json promise with fetch() in javascript

With the following code:

fetch(url, params).then(response => {
    return response.json();
}).then(json => {
    // json handler
    if (!response.ok) { // <-- Does not work, I don't have access to response object here
        if (json.error)
            throw new Error(json.error); // Application spesific error
        else
            throw new Error(`HTTP error ${response.statusText}(${response.status})`); // Generic error
    }
    console.log('Success:', json);
}).catch((e) => {
    console.error("Failed with:", e);
});

I have a problem where I can't get access to the response object from the json handler. I can't store response in a global variable because obviously that is a bad idea for many reasons. I can't use this as it simply points to window.

So what would be the proper way of passing the response object along to the then() handler of the json() promise in this case?

about 4 years ago · Juan Pablo Isaza
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