How to do it…
Let's start by seeing how a common error first callback works. The fs (file system) module provides a readFile() method that can read a file, and either produce its text or an error. My showFileLength1() function attempts to read a file, and list its length. As usual with callbacks, we have to provide a function, which will receive two values: a possible error, and a possible result.
This function must check whether the first argument is null or not. If it isn't null, it means there was a problem, and the operation wasn't successful. On the other hand, if the first argument is null, then the second argument has the file read operation result. The following code highlights the usual programming pattern used with Node callbacks; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access