The style of the Node.js core API

Earlier in this chapter, we learned how the reactor pattern and the non-blocking I/O model are two of the most fundamental characteristics of Node.js. This should help us to understand why callbacks are used so extensively in the entire Node.js core API. As we can imagine, the core API has a direct impact on all the other modules. As a result, the entire Node.js ecosystem uses callbacks extensively.

Node.js not only uses callbacks extensively, but it also uses them in a very consistent way:

  • Callbacks in Node.js are always the last argument of a function
  • Callbacks in Node.js always take an error as the first argument

The following code snippet uses the filesystem API to read a text file. The readFile function ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.