The Callback Pattern: Synchronous vs. Asynchronous (II)
Learn about synchronous APIs and asynchronicity with deferred execution.
We'll cover the following
Using synchronous APIs
The lesson to learn from the unleashing Zalgo example is that it is imperative for an API to clearly define its nature: either synchronous or asynchronous.
One possible fix for our inconsistentRead()
function is to make it completely synchronous. This is possible because Node.js provides a set of synchronous direct style APIs for most basic I/O operations. For example, we can use the fs.readFileSync()
function in place of its asynchronous counterpart. The code would become as follows:
Get hands-on with 1400+ tech skills courses.