Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Node.js Basics (2014) Building a Command Line Application Handling Parsing and Status Code Errors

Kevin Faust
Kevin Faust
15,353 Points

node.js try-catch block placement question

it makes more sense for me to use the first block of code. if we dont have a 200 status code, then dont run anything and just end the code. the instructor used the second block of code. which code would be better?

var request = https.get('https://teamtreehouse.com/' + username + '.json', function(response) {
  if (response.statusCode === 200) {
    var body = ""; 

    response.on('data', function(chunk) {
      body += chunk;
    });
    response.on('end', function() {
      try {
        var profile = JSON.parse(body);
        printMessage(profile.name, profile.badges.length, profile.points.JavaScript);
      } catch (e) {
        printError(e);
      }
    });
  } else {
    //error 
  }
});
var request = https.get('https://teamtreehouse.com/' + username + '.json', function(response) {

  var body = ""; 

  response.on('data', function(chunk) {
    body += chunk;
  });
  response.on('end', function() {
    if (response.statusCode === 200) {
      try {
        var profile = JSON.parse(body);
        printMessage(profile.name, profile.badges.length, profile.points.JavaScript);
      } catch (e) {
        printError(e);
      }
    } else {
      //error 
    }
  });
});

1 Answer

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hey Kevin Faust!

Great question. I did the exact same thing when I was developing this project. In the version of Node.js I was working on at the time it was timing out. The end event wasn't handle. Node.js' event loop would hang until it was handled. You'd have to trigger the end event manually in an undocumented method call to trigger the end.

The other way was to implement the end. Since this course was released Node has been through several versions and I've upgraded to the latest long term support of Node.js in Workspaces, so your code should work now. It looks like they've fixed that issue so it doesn't timeout and hang and end event handler can be avoided by the conditional code in the first example.

This would have been a massive technical tangent to explain but now it's seems to be resolved in the latest Node.js versions.

So yeah, use the first code if you want, just be super careful when using it in older version of Node.js.

Hope that helps!
Andrew

Kevin Faust
Kevin Faust
15,353 Points

hi andrew thanks. so basically in older versions of node, it needed that 'end' handler/thing otherwise the program would hang. and thats why you took the approach of the second code block?

is one code block faster than the other? if theyre both the same speed then i would use the second code block just for the extra support. but then again i only like to use new things/versions

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

That's exactly right.

Since it doesn't hang now I'd assume the first way would be slightly faster, but I don't know what it does to the connection, is it still hanging around because you haven't streamed the data or does the V8 engine or Node.js APIs do some code analysis and close the connection knowing it'll never be executed? What ever the answer is it looks like it's been optimized now since it doesn't hang and successfully falls through. I'd go with the first approach.