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

225
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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