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!');`
12 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.

8 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”.

1 Like

Same, there is a difference. I suspect though that the one person who doesn’t and those of us who do are using different browsers (I am using Chrome). Just speculation, I’m not qualified enough to know for certain. Wish though someone who clearly is qualified can add to this thread.

So I am confused with this setting the setTimeout to cero and still showing up at last. So conclusion even if you put time to cero still will show up last right? And the reason is because you included setTimeout? The system by default will put the console.log() at the end of the queue. correct?

On this lecture they refered us to go and visit this page to learn how event loops work: Concurrency model and the event loop - JavaScript | MDN

On that page they have this code:

function foo(b) {
  let a = 10
  return a + b + 11
}

function bar(x) {
  let y = 3
  return foo(x * y)
}

const baz = bar(7) // assigns 42 to baz

But I am sorry can someone explain me how bar(7) is igual to 42?
As far as my algebra little knowledge I can remember.

foo(x * y) = 21*(21) = 21 x 21 not 21 + 21