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

175
Views
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
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!