Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a Simple Dynamic Site with Node.js Creating a Simple Server in Node.js HTTP Server Review

Quiz question. What is the output from the following code?

var http = require("http");

http.createServer(function(request, response) { response.writeHead(200, {'Content-Type': 'text/plain'});

setTimeout(function(){ response.end('Goodbye World\n'); }, 1000);

response.write("Hello World\n"); }).listen(3000);

A. Goodbye World

B. Hello World Goodbye World

C. Hello World

D. Goodbye World Hello World

my choice was C. but, I received this message. Bummer! unfortunately, that answer is in correct. Why?. Hello World was the last thing printed, so doesn't a server always write out the last piece of code to the page? Which in this case would be Hello World.

1 Answer

Miguel Canas
seal-mask
.a{fill-rule:evenodd;}techdegree
Miguel Canas
Python Development Techdegree Student 11,470 Points

Hey Jason,

Checkout the third line of code...

var http = require("http");

http.createServer(function(request, response) { response.writeHead(200, {'Content-Type': 'text/plain'});

setTimeout(function(){ response.end('Goodbye World\n'); }, 1000);

response.write("Hello World\n"); }).listen(3000);

... The setTimeout([function], [milliseconds]) function will execute the given function after the given number of milliseconds. In this case, the response.write call for "Goodbye World" will delay execution for 1000 milliseconds, and the "Hello World" message will execute immediately.

Hence the correct answer would be B.

Make sense?

~ Miguel

Yes, I understand that now. Thanks.