Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda