The Problem
Uncover the pitfalls of testing the asynchronous code with Jest.
Synchronous vs. asynchronous
The words “synchronous” and “asynchronous” are likely used often when discussing programming. They describe an important part of how our code executes. In synchronous programming languages, such as Java and Python, a line or block of code does not begin to execute until the previous line or block in progress has been completed successfully. In other words, how we read the code reflects the order in which it runs. It’s fairly intuitive.
Asynchronous code execution is less intuitive. In an asynchronous language, other code can begin to execute while the previous code is still waiting to complete. This “other” code can potentially complete before the initial code. This early completion can lead to unintended consequences and a flow that cannot be relied upon.
This typically occurs with long-running processes and requests to external resources for data, i.e., an HTTP request. So, if a request is made, and the program is waiting for the response but not doing anything, subsequent code will continue to execute.
Get hands-on with 1200+ tech skills courses.