O'Reilly logo

The Node Craftsman Book by Manuel Kiessling

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required