Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added callback-wrapping feature #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

prust
Copy link

@prust prust commented Mar 2, 2013

@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
Copy link
Author

prust commented Dec 5, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant