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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.