The Callback Pattern: Synchronous vs. Asynchronous (I)

Learn about sync and async functions, and how to solve unpredictable function problems using zalgo.

Synchronous or asynchronous?

We’ve seen how the execution order of the instructions changes radically depending on the nature of a function—synchronous or asynchronous. This has strong repercussions on the flow of the entire application, both in terms of correctness and efficiency. The following is an analysis of these two paradigms and their pitfalls. In general, what must be avoided is creating inconsistency and confusion around the nature of an API because doing so can lead to a set of problems that might be very hard to detect and reproduce. To drive our analysis, we’ll take, as an example, the case of an inconsistently asynchronous function.

An unpredictable function

One of the most dangerous situations is to have an API that behaves synchronously under certain conditions and asynchronously under others. Let’s take the following code as an example:

Get hands-on with 1200+ tech skills courses.