Skip to content

Instantly share code, notes, and snippets.

@raynerpupo
Last active April 17, 2018 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raynerpupo/93a84e6fc06e4888adceb9b0780a3bb3 to your computer and use it in GitHub Desktop.
Save raynerpupo/93a84e6fc06e4888adceb9b0780a3bb3 to your computer and use it in GitHub Desktop.

Node Error first callback

The expected callback function

This kind of callback function have the error as the first parameter and the actual value as the second

function incrementAndLog(err, value) {
  if (err) {
    console.log("there's no value to increment");
    return;
  }
  console.log(value + 1);
}

The caller function

The caller function will call the callback by setting the first parameter if there's some error or the second one to give the result.

function paranoidDivisionMax100(x, y, cb) {
  if (y < 1) {
    return cb("y should be greater than zero");
  }
  if (y > 100) {
    return cb("y should be less than 101");
  }
  return cb(null, x / y);
}

So if we call

paranoidDivisionMax100(8, 2, incrementAndLog)

It will print the 5 value, but if we call:

paranoidDivisionMax100(8, 0, incrementAndLog)

it will print "there's no value to increment"

Error propagation

We can also take care of some errors and let a outer function to handle the others.

function calculate(x, y, next) {
  return paranoidDivisionMax100(x, y, (err, result) => {
    if (err === "y should be greater than zero") {
      console.log(err);
      return;
    }
    if (err) {
      return next(err);
    }
    return result;
  });
}

Here we propagate the unexpected errors through the next callback that could be defined as:

function handleWeirdError(err) {
  console.log(`Ups we had a weird error: ${err}`);
  return;
}

Unhandled exceptions

We can still use the built in try and catch to handle any exeption thrown on the callbacks code

Using Generators

By using generators we can pause the function execution while we wait for an asynchronous operation to finish. There are some libraries that help us to intercept callbacks an resume the execution when the result is ready like Suspend.

Using our function paranoidDivisionMax100 we could write this.

// Import the Suspend library
var suspend = require('suspend');

suspend(function*(resume) {
// We will wait until the function 'resume' call next() to
// continue with the execution after the callback function is done. 
  var data = yield paranoidDivisionMax100(8, 2, resume);
// The first element of the list contains the error if any
  if(data[0]) {
    throw data[0];
  }
// The second element contains the actual result
  console.log(data[1]);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment