Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8203547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:35:42+00:00 2026-06-07T07:35:42+00:00

I am a new programmer whose primary background is in Java. I am attempting

  • 0

I am a new programmer whose primary background is in Java. I am attempting to write in fault handling to a program in Javascript as I would in Java. In java I use the Apache HTTP client to both create the client and call the Httpget request.

 HttpClient cli = new DefaultHttpClient();
 cli.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
 HttpResponse resp = null;
 for (int i = 0 ; i < 5 ; i++) {
 try {
 resp = cli.execute(new HttpGet("http://example.org/products"));
 }
 catch{//code}
 }

I am unsure how to emulate this behavior in a javascript environment. Does anyone have insight or knowledge into this field?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:35:44+00:00Added an answer on June 7, 2026 at 7:35 am

    Here’s the exact equivalent of your code snippet, but in JavaScript!

     var cli;
     var callback = function(resp) {
        // due to the asynchronous nature of XMLHttpRequest, you'll need to put all logic that uses the response in a callback function.
        // code below using responseText
        console.log(resp);
     };
     var timeout = 5000;
     var handler = function() {
        var errorSeries;
        if (this.readyState === 4) { // indicates complete
            errorSeries = parseInt(this.status.toString().charAt(0)); // will be "2" or "3" for 200 or 300 series responses
            if (errorSeries === 2 || errorSeries === 3) {
                callback.call(this, this.responseText);
            } else {
                // handle http error here
            }
        }
     }
     for (var i = 0 ; i < 5 ; i++) {
     cli = new XMLHttpRequest(); // ActiveXObject('Microsoft.XMLHTTP') in IE8 and below
     cli.timeout = timeout;
     cli.onreadystatechange = handler;
     try {
        cli.open('GET','http://example.org/products');
        cli.send();
     }
     catch(e) {
     }
    

    If the above looks wordy, that’s because it is. Other commenters have steered you right: look into using a library like jQuery to abstract away this kind of boilerplate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a relatively new java programmer and I've been tinkering around with this program
Possible Duplicate: C++: When to use References vs. Pointers I'm pretty new programmer to
I'm still a relatively new programmer, and an issue I keep having in Java
I'm a new C++ programmer, I learned Java and ANSI C a time ago
I am very new programmer to Java regular expressions. I do not want to
New programmer here with only some minor Java experience trying my hand at writing
I'm a new programmer learning JavaScript for a school project. I have two variables,
New programmer here, I am trying to understand and break down this code below
So I am a new programmer and I just installed XCode on my Macbook
I am a very new programmer, I have made a couple basic applications, however

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.