async-stacktrace icon indicating copy to clipboard operation
async-stacktrace copied to clipboard

Added callback-wrapping feature

Open prust opened this issue 12 years ago • 1 comments

@Pita: I'm not sure if you're interested in this feature, but it will be useful to us, so I thought I would open a pull request as an FYI.

Occasionally in our code base it is convenient to pass the callback function along directly, without wrapping it in another anonymous function -- for instance in situations like this:

function getUsers(db, callback) {
  var sql = 'SELECT * FROM users';
  db.query(sql, callback);
}

In order to use ERR(), we would have to add an anonymous function, like this:

function getUsers(db, callback) {
  var sql = 'SELECT * FROM users';
  db.query(sql, function(err, data) {
    if (ERR(err, callback)) return;
    callback(null, data);
  });
}

This pull request allows us to instead wrap the callback with ERR(), like this:

function getUsers(db, callback) {
  var sql = 'SELECT * FROM users';
  db.query(sql, ERR(callback));
}

The stacktrace line is cached at the moment of wrapping and re-used later if an error object is passed to the wrapped callback function.

I added a couple of tests and some documentation. Thanks for your work on this project!

prust avatar Mar 02 '13 07:03 prust

@Pita: Would you mind picking a license (MIT would be nice) so there aren't legal questions about using your code?

Also, are you interested in this pull request or in https://github.com/Pita/async-stacktrace/pull/3? If you would prefer a different API or syntax, I'm wide open to suggestions. If you're not interested in either, I'll probably rename my fork & maintain it independently, since our team has been increasingly relying on & enjoying the callback-wrapping feature...

prust avatar Dec 05 '13 19:12 prust