Skip to content

Instantly share code, notes, and snippets.

@jdx
Created August 1, 2014 20:54
Show Gist options
  • Save jdx/93787b9fc8a1ba8cd70a to your computer and use it in GitHub Desktop.
Save jdx/93787b9fc8a1ba8cd70a to your computer and use it in GitHub Desktop.
promise error handling vs callback error handling

Error checking with callbacks:

dbFind('foo', function(err, data) {
  if (err) throw err;
  dbFind(data.id, function(err, data) {
    if (err) throw err;
    dbFind(data.id, function(err, data) {
      if (err) throw err;
        dbFind(data.id, function(err, data) {
          if (err) throw err;
          console.log(data);
        });
      });
    });
  });
});

Error handling with promises (using q):

Q.fcall(dbFind, 'foo')
.then(function (data) {
  return Q.fcall(dbFind, data.id)
})
.then(function (data) {
  return Q.fcall(dbFind, data.id)
})
.then(function (data) {
  return Q.fcall(dbFind, data.id)
})
.then(function (data) {
  console.log(data);
})
.catch(function (err) {
  throw err;
})
.done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment