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

282
Visualizações
Get http response status code after response.json()

I would like to get http status code after response.json to use it in my logic later, can I do something with it?

function apiRequest(path, options) {
    fetch("api/" + path, options)
        .then(response => response.json())
        .then(data => {
            let res = {
                code: 200 //I want to put http status code here,
                data: data
            }

            return res;
        })
}

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This is slightly tricky using then (as you are currently doing) because you want to get data directly from the response (which is a promise) and some more data from the parsed body (which is another promise).

So you can wrap the status and the data promise in a Promise.all and return that from the first then:

const apiRequest = () => {
  const url = "//swapi.dev/api/planets/1/";
  fetch(url)
    .then((response) => Promise.all([response.status, response.json()]))
    .then(([status, data]) => console.log({status, data}))
}

… but it would be easier to use async/await syntax and ditch the callbacks and you then only have to worry about a single function (and therefore scope) rather than multiple.

const apiRequest = async () => {
  const url = "//swapi.dev/api/planets/1/";
  const response = await fetch(url);
  const data = await response.json();
  const status = response.status;
  console.log({status, data})
}
about 4 years ago · Juan Pablo Isaza Relatório

0

As an alternative you could consider async/await. That way you have access to response and data at the same time more easily.

async function apiRequest(path, options) {
    const response = await fetch("api/" + path, options)
    const data = await response.json()

    let res = {
         code: response.status,
         data: data
    }

    // Do something with res
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this

function apiRequest(path, options) {
    fetch("api/" + path, options)
        .then(response => Promise.all([Promise.resolve(response.status), response.json()]))
        .then(([status, data]) => {
            let res = {
                code: status //I want to put http status code here,
                data: data
            }

            return res;
        })
}

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