search

Q

Q is a promise library providing many more utilities than the native Promise implementation.

List of features:

Real world example from a Node.js application:

// Traditional implementation
function authenticate (req, res, next) {
  User.findOne({ id: req.id }, function (err, user) {
    if (err) {
      return next('not found');
    }

    user.save(function (err, response) {
      // ...
    });
  });
}

// Q Based
function authenticate(req, res, next) {
  return Q(req.id)
    .then(function (id) {
       return Q.nfcall(User, 'findOne', id);
    })
    .then(function (user) {
       return Q.ninvoke(user, 'save');
    })
    .then(function (saved) {
       return res.send(201);
    })
    .catch(function (err) {
      return next(err);
    });
}