Introduction

By now, you are probably more than familiar with this:

someFunction(function(err) { 
  if (!err) {
    console.log('Hey, looks like someFunction has finished and called   
    me.');
  } else {
    console.log('Oh no, something terrible happened!');
  } 
});

We call a function, someFunction in this case, which does something asynchronously in the background, and calls the anonymous function we passed in (the callback) once it has finished, passing an Error object if something went wrong, or null if all went fine. That's the standard callback pattern, and you will encounter it regularly when writing Node.js code.

For simple use cases, where we call a function that finishes its task some time in the future, either successfully or with an error, this ...

Get The Node Craftsman Book 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.