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

221
Visualizações
How to take two fetches and compare their data as variables whilst not getting any errors?

I want to take two different fetches, put them into a variable form so their important data can be used for something else other than just logging the data.

I'm trying to do this via window response async, however, I am currently at a dead end because though what I'm doing works on one strand of data, it doesn't work on two because of the JSON body stream already read error.

let RESPONSE = window.Response.prototype.json;
window.Response.prototype.json = async function () {
  if (!('https://a/')) return RESPONSE.call(this)
  let x = await RESPONSE.call(this);
  if (!('https://b/')) return RESPONSE.call(this)
  let y = await RESPONSE.call(this);
  for (let detect in x) {
    if (x[detect] !== y[detect]) {
      console.log(x[detect]);
      console.log(y[detect]);
    }
  }
  return x;
  return y;
};

How can I keep the data in a variable form that can be used for something like this:

for (let detect in x) {
  if (x[detect] !== y[detect]) {
    console.log(x[detect]);
    console.log(y[detect]);
  }

but whilst being able to have both variables defined at the same time? This would mean I would need to get past the body stream error while also keeping that core code. How can I do that?

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

0

Does this help you?

async function doTwoRequestsAndCompareStatus() {
  const res1 = await fetch("https://fakejsonapi.com/fake-api/employee/api/v1/employees");
  const res2 = await fetch("https://fakejsonapi.com/fake-api/employee/api/v1/employees");
  const data1 = await res1.json();
  const data2 = await res2.json();

  console.log('both were equal?', data1.status === data2.status);
}

// Don't forget the actually call the function 😉
doTwoRequestsAndCompareStatus();

Although I would recommend this, both because it's cleaner and faster, as the fetches are executed at the same time and not sequentially.

async function doTwoRequestsAndCompareStatus() {
  const [data1, data2] = await Promise.all([
    fetch("https://fakejsonapi.com/fake-api/employee/api/v1/employees").then(r => r.json()),
    fetch("https://fakejsonapi.com/fake-api/employee/api/v1/employees").then(r => r.json()),
  ]);

  console.log('both were equal?', data1.status === data2.status);
}

// Don't forget the actually call the function 😉
doTwoRequestsAndCompareStatus();

If you find the first one easier to understand though, I would recommend using it 👍.

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