Learn
Requests I
HTTP Requests

One of JavaScript’s greatest assets is its non-blocking properties, or that it is an asynchronous language.

Websites, like newspaper websites, take advantage of these non-blocking properties to provide a better user experience. Generally, a site’s code is written so that users don’t have to wait for a giant image to load before being allowed to read the actual article—rather, that text is rendered first and then the image can load in the background.

JavaScript uses an event loop to handle asynchronous function calls. When a program is run, function calls are made and added to a stack. The functions that make requests that need to wait for servers to respond then get sent to a separate queue. Once the stack has cleared, then the functions in the queue are executed.

Web developers use the event loop to create a smoother browsing experience by deciding when to call functions and how to handle asynchronous events. We’ll be exploring one system of technologies called Asynchronous JavaScript and XML, or AJAX.

To read more about the event loop, read the MDN documentation:

Instructions

1.

To get a glimpse of how the event loop works, take a look at the code in the code editor.

We’ll be using setTimeout(), which will pass a function call to the queue. The first argument is a callback and the second argument is the number of milliseconds the program must wait before the callback can be run.

The other console.log() calls are run from the stack.

2.

Interesting right?

What if we change the 2500 in setTimeout() to be 0? Essentially the callback doesn’t need to wait before it can be called. Do you think that this change will affect the order?

Folder Icon

Take this course for free

Already have an account?