Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!