FAQ: Requests I - HTTP Requests

This community-built FAQ covers the “HTTP Requests” exercise from the lesson “Requests I”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise HTTP Requests

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello For this one, i thought only the line within the setTimeout would be delayed? But actually all three lines are delayed, why is that? Thanks!

8 Likes

Is AJAX a built-in feature of JavaScript? Is AJAX used only for requests or does it have other applications?

Why does the console.log statement within the setTimeout function still occur last, even after you change the timeout to 0?

`console.log('First message!');
setTimeout(() => {
   console.log('This message will always run last...');
}, 0);
console.log('Second message!');`
13 Likes

cthunda303. Once you use setTimeout(), in pratice you can hardly ever have 0 milliseconds. Due to admin reasons the time is always approximate, so 0 does not really mean zero precisely.

6 Likes

Thanks,I didn’t know that.

Because of the event loop. The seTimeout got assigned to a seperate queue.

4 Likes

I noticed this too! So I put the following what-happens-if code into main.js and also in Visual Code.
When I run the script from within codecademy, it takes about 4 seconds before any lines of output are printed. When I run from VisualCode, the first two lines immediately print to the console. I suspect running from Visual Code is more accurate. I am not sure what codecademy does to invoke a script when you hit the Run button

console.log(‘First message!’);
setTimeout(() => {
console.log(‘This message will run third…’);
}, 3000);
setTimeout(() => {
console.log(‘This message will run fourth…’);
}, 4000);

console.log(‘Second message!’);

2 Likes

I came here wondering the same thing. It seems to be an issue with Codecademy. When I test it in a text editor and run it on the command line (Debian 10), the messages without setTimeout print immediately.

Actually, I don’t see that effect. The code behaves as expected either in VSC or Codeacademy . the console.log should run last because is being putting in a queue using “setTimeout”.