0.0.1 • Published 10 years ago

errcb v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

errcb

Error callback wrapper.

Installation

$ npm install errcb

Running Tests

Install dev dependencies.

$ npm install

Run tests.

$ npm test

Check code quality.

$ npm run lint

Example

'use strict';

// instead of this:

app.get('/renamefoo', function (req, res, next) {
  FooModel.findOne({ name: req.query.oldname }, function (err, oldfoo) {
    if (err) {
      next(err);
    } else {
      oldfoo.name = req.query.newname;
      oldfoo.save(function (err, newfoo) {
        if (err) {
          next(err);
        } else {
          res.json(newfoo);
        }
      });
    }
  });
};

// you can write this:

app.get('/renamefoo', function (req, res, next) {
  FooModel.findOne({ name: req.query.oldname }, errcb(next, function (oldfoo) {
    oldfoo.name = req.query.newname;
    oldfoo.save(errcb(next, function (newfoo) {
      res.json(newfoo);
    });
  }));
});

// or this:

app.get('/renamefoo', function (req, res, next) {
  var myerrcb = errcb.bind(null, next);
  FooModel.findOne({ name: req.query.oldname }, myerrcb(function (oldfoo) {
    oldfoo.name = req.query.newname;
    oldfoo.save(myerrcb(function (newfoo) {
      res.json(newfoo);
    }));
  }));
});

License

MIT