Let’s start by simplifying the difference between sync and async code.
Synchronous means the code runs right now.
Asynchronous means the code will run later.
What about asynchronous code? When have we used that? A prime example is when our application needs user input to perform a desired behavior. When our application loads, we don’t know when a user will click a button or fill out fields, but it will certainly happen later, not right now.
setInterval(), two functions that add asynchrony to our applications. Then we’ll learn how to make API calls. API calls involve making a request from another server and then waiting for a response from that server. The wait is what makes an API call asynchronous; we won’t get a result back from the server until later (even if the call only takes a fraction of a second to process). We’ll also learn more about using callbacks and promises, two important tools for working with asynchrony.
Imagine you throw a ball in the air, but you can only throw one a time. This is the equivalent of single-threaded.
However, once you throw a ball in the air, you can throw a second ball before the first lands. That’s the equivalent of non-blocking. If you didn’t have this non-blocking capacity, however, you wouldn’t be able to do anything else while a ball is in the air. You wouldn’t be able to throw another ball or move around or do anything at all. In other words, you’d freeze up.
Ouch. Nobody likes when the browser freezes up. It’s one of the most frustrating UI experiences a user can have.
setInterval(), which we’ll do in the next lesson.